懒汉式
Java
public class Singleton{
private static Singleton instance;
private Singleton(){ }
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
Kotlin
class Singleton private constructor(){
companion object{
private var instance :Singleton? = null
get(){
if(field == null) field = Singleton()
return field
}
fun get():Singleton = install!!
}
}