这个写法完全是Java静态内部类实现单例的写法。
- kotlin写法
class Single private constructor() {
companion object {
fun getInstance(): Single {
return Holder.INSTANCE
}
}
private object Holder {
val INSTANCE = Single()
}
}
- Java写法
class Single {
private Single() {
}
public static Single getInstance() {
return Holder.INSTANCE;
}
private static class Holder {
private static Single INSTANCE = new Single();
}
}