Kotlin 对象 class 实例 get()
Kotlin Object class instance get()
public class Singleton {
private static Singleton instance = null;
private Singleton(){
}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
doSomeInitializationIfRequired();
return instance;
}
}
这里的 getInstance() 方法在我们请求实例时被调用,如果我们想在每次从任何地方调用实例时做一些事情,我们可以添加代码。
有没有什么方法可以像这样使用 Kotlin 对象覆盖实例变量 get()
例如
object SomeSingleton {
get() = {
doSomeInitializationIfRequired()
}
}
我知道我会写
init {
}
但这只会被调用一次。
你可以使用一点抽象,创建一个 interface
(随便你给它起什么名字)。并定义所有可能的方法和属性,单例应该有:
interface ISomeSingleton {
fun something()
}
然后创建一个私有实现作为单例:
private object SingletonImpl : ISomeSingleton {
override fun something() = println("hello")
}
最后用自定义 getter 定义一个顶级字段,returns 实现:
val SomeSingleton: ISomeSingleton
get() {
// do your fancy stuff here
return SingletonImpl
}
现在您可以通过 SomeSingleton
字段获取 ISomeSingleton
的实例。而你的初始化代码总是运行
您可以为 instance
属性 创建一个自定义 getter
函数,并在那里添加您的额外检查
object Singleton {
val instance: Singleton
get() {
println("doing something extra")
return this
}
}
对于这种类型的单例,我不会使用 object
。你可以像 Java 那样做。
class Singleton private constructor() {
companion object {
private val _instance = Singleton()
val instance: Singleton
get() = synchronized(this) {
doSomeInitializationIfRequired()
_instance
}
}
}
如果您需要构造函数或 doSomeInitializationIfRequired()
的参数,您可以将 val instance
替换为 getInstance
函数。这在 Android 上很常见,您的单例可能需要引用 Application 实例。
class Singleton private constructor(val application: Application) {
companion object {
private val instance: Singleton? = null
fun getInstance(application: Application): Singleton = synchronized(this) {
val value = instance ?: Singleton(application).also { instance = it }
doSomeInitializationIfRequired(application)
value
}
}
}
public class Singleton {
private static Singleton instance = null;
private Singleton(){
}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
doSomeInitializationIfRequired();
return instance;
}
}
这里的 getInstance() 方法在我们请求实例时被调用,如果我们想在每次从任何地方调用实例时做一些事情,我们可以添加代码。
有没有什么方法可以像这样使用 Kotlin 对象覆盖实例变量 get()
例如
object SomeSingleton {
get() = {
doSomeInitializationIfRequired()
}
}
我知道我会写
init {
}
但这只会被调用一次。
你可以使用一点抽象,创建一个 interface
(随便你给它起什么名字)。并定义所有可能的方法和属性,单例应该有:
interface ISomeSingleton {
fun something()
}
然后创建一个私有实现作为单例:
private object SingletonImpl : ISomeSingleton {
override fun something() = println("hello")
}
最后用自定义 getter 定义一个顶级字段,returns 实现:
val SomeSingleton: ISomeSingleton
get() {
// do your fancy stuff here
return SingletonImpl
}
现在您可以通过 SomeSingleton
字段获取 ISomeSingleton
的实例。而你的初始化代码总是运行
您可以为 instance
属性 创建一个自定义 getter
函数,并在那里添加您的额外检查
object Singleton {
val instance: Singleton
get() {
println("doing something extra")
return this
}
}
对于这种类型的单例,我不会使用 object
。你可以像 Java 那样做。
class Singleton private constructor() {
companion object {
private val _instance = Singleton()
val instance: Singleton
get() = synchronized(this) {
doSomeInitializationIfRequired()
_instance
}
}
}
如果您需要构造函数或 doSomeInitializationIfRequired()
的参数,您可以将 val instance
替换为 getInstance
函数。这在 Android 上很常见,您的单例可能需要引用 Application 实例。
class Singleton private constructor(val application: Application) {
companion object {
private val instance: Singleton? = null
fun getInstance(application: Application): Singleton = synchronized(this) {
val value = instance ?: Singleton(application).also { instance = it }
doSomeInitializationIfRequired(application)
value
}
}
}