Java与Kotlin单例模式

5种常见单例

  • 饿汉式
  • 懒汉式
  • 同步锁式
  • 双重检查式(double-check)
  • 内部类式

1. 饿汉式

优点:简单好写,线程安全
缺点:类加载就初始化了对象

  • java写法
public class Singleton {
    private static Singleton  singletonInstance= new Singleton ();

    private Singleton (){}

    public static Singleton getInstance(){
        return singletonInstance;
    }
}
  • kotlin写法
object Singleton{
} 

2.懒汉式

优点:第一次使用时,才初始化对象
缺点:线程非安全,多线程中可能会创建多个对象

  • java写法
public class Singleton {
    private static Singleton  singletonInstance;

    private Singleton (){}

    public static Singleton getInstance(){
        if(singletonInstance== null){
             singletonInstance= new Singleton ()
        }
        return singletonInstance;
    }
}
  • kotlin写法
class Singleton private constructor(){
    companion object{
        val singletonInstance: Singleton by lazy { Singleton () }
    }
}

3.同步锁式

优点:保证线程安全
缺点:每次都要加锁

  • java写法
public class Singleton {
    private static Singleton  singletonInstance;

    private Singleton (){}

    public static Synchronized Singleton getInstance(){
        if(singletonInstance== null){
             singletonInstance= new Singleton ()
        }
        return singletonInstance;
    }
}
  • kotlin写法
class Singleton private constructor(){
    companion object {
        private var singletonInstance: Singleton? = null

        @Synchronized
        fun getInstance(): Singleton? {
            if (singletonInstance == null) {
                singletonInstance = Singleton()
            }
            return singletonInstance!!
        }
    }
}

4.double-check式

  • java写法
public class Singleton {
    private static volatile Singleton  singletonInstance;

    private Singleton (){}

    public static Singleton getInstance(){
        if(singletonInstance== null){
            synchronized(Singleton.class)
                if(singletonInstance== null){
                    singletonInstance= new Singleton ()
                }
          }
          return singletonInstance;
    }
}
  • kotlin写法
class Singleton private constructor(){
    companion object{
        val singletonInstance: Singleton by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            Singleton () }
    }
}

java变种写法
class Singleton private constructor(){
    companion object {
        @Volatile
        private var singletonInstance: Singleton? = null

        fun getInstance(): Singleton? {
            if (singletonInstance == null) {
                synchronized(Singleton::class.java){
                    if (singletonInstance == null) {
                        singletonInstance = Singleton()
                    }
                }
            }
            return singletonInstance
        }
    }
}

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.singletonInstance
    }

    private object Holder{
        val singletonInstance= Singleton ()
    }

}

你可能感兴趣的:(Java与Kotlin单例模式)