Kotlin 单例

1.懒汉模式

java

public class Singleton {

    private static Singleton singleton = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return singleton;
    }
}

kotlin

object Singleton{}
2.懒加载(非线程安全)

java

public class Singleton {

    private static Singleton instance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

}

kotlin

// 1 //
class Singleton {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.NONE) {
            Singleton()
        }
    }
}

// 2 //
class Singleton {

    private var instance: Singleton? = null

    fun get(): Singleton {
        if (instance == null) {
            instance = Singleton()
        }
        return instance!!
    }
}
3.懒加载(线程安全)

java

public class Singleton {

    private static Singleton instance;

    private Singleton() {
    }

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

kotlin

class Singleton private constructor() {

    private var instance: Singleton? = null

    @Synchronized
    fun get(): Singleton {
        if (instance == null) {
            instance = Singleton()
        }
        return instance!!
    }
}
4.双重锁

java

public class Singleton {

    private static volatile Singleton instance;

    private Singleton() {
    }

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }

}

kotlin

// 1 //
class Singleton private constructor() {

    companion object {
        val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            Singleton()
        }
    }
 }
// 2 //
class Singleton private constructor() {

    private @Volatile var instance: Singleton? = null

    fun get(): Singleton {
        if (instance == null) {
            synchronized(this) {
                if (instance == null) {
                    instance = Singleton()
                }
            }
        }
        return instance!!
    }
}
5.最优雅的写法:静态内部类

java

public class Singleton {

    private static class Holder {
        private static Singleton instance = new Singleton();
    }

    private Singleton() {
    }

    public static Singleton getInstance() {
        return Holder.instance;
    }
}

kotlin

class Singleton private constructor() {

    companion object {
        fun getInstance() = Holder.instance
    }

    private object Holder {
        val instance = Singleton()
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • kotlin中的单例 class SingletonTestKt{ } /*//懒加载object Singl...
    Derek_Yan阅读 845评论 0 2
  • 对象声明就是单例了。object DataProviderManager就可以理解为创建了类DataProvide...
    tesla1984阅读 836评论 0 0
  • 文/ZYRzyr原文链接:http://www.jianshu.com/p/b314ddce4d69 本文列举了几...
    ZYRzyr阅读 2,387评论 4 51
  • 单例的实现方法,可以通过同伴对象,或者 lazy。示例: 通过 lazy 实现 参考《Programming Ko...
    jinkui阅读 828评论 0 1
  • 作为一个经历了残酷秋招的应届生,基本上在很多企业面试中都经历过无领导小组讨论这个环节。面临马上要到来的更加惨无人道...
    蓝色锅盖阅读 8,523评论 0 5