Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 439 Bytes

Kotlin实现单例模式.md

File metadata and controls

27 lines (22 loc) · 439 Bytes

Kotlin实现单例模式

静态内部类 · 单例模式

class B private constructor() {
    companion object {
        fun getInstance() = Holder.sIntance
    }

    private object Holder {
        val sIntance = B()
    }
}

同步懒加载 · 单例模式

class A {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            A()
        }
    }
}