Kotlin/Native:有人可以帮我建议如何创建一个接受初始化参数的冻结单例吗?
Kotlin/Native: Can someone help me in suggesting how to create a frozen singleton that takes in init parameters?
下面的代码将不起作用,因为一旦我们尝试像 SomeClass.getInstance()
或它的任何其他 属性 那样访问它,就会创建伴随对象并使其不可变,我将无法初始化 someClass
属性。我希望它是 immutable/frozen (因为这将被多个线程访问)但是当它需要参数时我不能这样做。
有什么建议吗?
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClass: SomeClass? = null
fun initialize(someProperty: SomeProperty){
someClass = SomeClass(someProperty)
}
actual fun getInstance(): SomeClass {
if (someClass == null) {
throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
return someClass as SomeClass
}
}
}
一种可能的解决方案是在此处使用 AtomicReference。像这样:
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClassAtomicRef: AtomicReference<SomeClass?> = AtomicReference(null)
fun initialize(someProperty: SomeProperty){
val someClass = SomeClass(someProperty)
someClassAtomicRef.value = someClass.freeze()
}
actual fun getInstance(): SomeClass {
return someClassAtomicRef.value ?: throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
}
}
上面代码的问题是我可以再次调用 SomeClass.initialise
并在 AtomicReference 中有另一个 SomeClass 实例。
有没有更好的方法来实现这个?
原子引用是这里的方法
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClassAtomicRef: AtomicReference<SomeClass?> = AtomicReference(null)
fun initialize(someProperty: SomeProperty){
val someClass = SomeClass(someProperty)
someClassAtomicRef.compareAndSet(null, someClass.freeze())
}
actual fun getInstance(): SomeClass {
return someClassAtomicRef.value ?: throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
}
}
下面的代码将不起作用,因为一旦我们尝试像 SomeClass.getInstance()
或它的任何其他 属性 那样访问它,就会创建伴随对象并使其不可变,我将无法初始化 someClass
属性。我希望它是 immutable/frozen (因为这将被多个线程访问)但是当它需要参数时我不能这样做。
有什么建议吗?
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClass: SomeClass? = null
fun initialize(someProperty: SomeProperty){
someClass = SomeClass(someProperty)
}
actual fun getInstance(): SomeClass {
if (someClass == null) {
throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
return someClass as SomeClass
}
}
}
一种可能的解决方案是在此处使用 AtomicReference。像这样:
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClassAtomicRef: AtomicReference<SomeClass?> = AtomicReference(null)
fun initialize(someProperty: SomeProperty){
val someClass = SomeClass(someProperty)
someClassAtomicRef.value = someClass.freeze()
}
actual fun getInstance(): SomeClass {
return someClassAtomicRef.value ?: throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
}
}
上面代码的问题是我可以再次调用 SomeClass.initialise
并在 AtomicReference 中有另一个 SomeClass 实例。
有没有更好的方法来实现这个?
原子引用是这里的方法
actual open class SomeClass private constructor(private val someProperty: SomeProperty) {
actual companion object {
private var someClassAtomicRef: AtomicReference<SomeClass?> = AtomicReference(null)
fun initialize(someProperty: SomeProperty){
val someClass = SomeClass(someProperty)
someClassAtomicRef.compareAndSet(null, someClass.freeze())
}
actual fun getInstance(): SomeClass {
return someClassAtomicRef.value ?: throw UninitializedPropertyAccessException("SomeClass is not initialised yet")
}
}
}