Kotlin单例代码实例

目录

一、饿汉式的实现

Kotlin版本

object SingletonDemoKt

/**
 *  背后的逻辑代码:
    public final class SingletonDemoKt {
        public static final SingletonDemoKt INSTANCE;

        private SingletonDemoKt() {
        }

        static {
            SingletonDemoKt var0 = new SingletonDemoKt();
            INSTANCE = var0;
        }
    }
 */

Java版本

public class SingletonDemo {
    private static SingletonDemo mInstance = new SingletonDemo();
    private SingletonDemo() {}
    public static SingletonDemo getInstance() {
        return mInstance;
    }
}

二、懒汉式的实现

Kotlin版本

class SingletonDemo2Kt {
    companion object {
        private var mInstance : SingletonDemo2Kt? = null
            get() {
                if (field == null) {
                    field = SingletonDemo2Kt()
                }
                return field
            }
        fun getInstance() = mInstance!!
    }
    fun show() {
        println("SingletonDemo2Kt show")
    }
}

fun main() {
    SingletonDemo2Kt.getInstance().show()
}


/**
 * 背后逻辑代码:
    public final class SingletonDemo2Kt {
        private static SingletonDemo2Kt mInstance;
        public static final SingletonDemo2Kt.Companion Companion = new SingletonDemo2Kt.Companion((DefaultConstructorMarker)null);

        public final void show() {
            String var1 = "SingletonDemo2Kt show";
            boolean var2 = false;
            System.out.println(var1);
        }
        ...
        public static final class Companion {
            private final SingletonDemo2Kt getMInstance() {
                if (SingletonDemo2Kt.mInstance == null) {
                    SingletonDemo2Kt.mInstance = new SingletonDemo2Kt();
                }

            return SingletonDemo2Kt.mInstance;
        }

        private final void setMInstance(SingletonDemo2Kt var1) {
            SingletonDemo2Kt.mInstance = var1;
        }

        @NotNull
        public final SingletonDemo2Kt getInstance() {
            SingletonDemo2Kt var10000 = ((SingletonDemo2Kt.Companion)this).getMInstance();
            if (var10000 == null) {
                Intrinsics.throwNpe();
            }

            return var10000;
        }

        private Companion() {
        }

        // $FF: synthetic method
        public Companion(DefaultConstructorMarker $constructor_marker) {
            this();
        }
        }
    }
  */

Java版本

public class SingletonDemo2 {
    private static SingletonDemo2 mInstance;
    private SingletonDemo2() {}

    public static SingletonDemo2 getInstance() {
        if (mInstance == null) {
            mInstance = new SingletonDemo2();
        }
        return mInstance;
    }

    public static void show() {
        System.out.println("SingletonDemo2 show");
    }

    public static void main(String[] args) {
        SingletonDemo2.getInstance().show();
    }
}

三、安全 懒汉式的实现

Kotlin实现

class SingletonDemo3Kt {
    companion object {
        private var mInstance : SingletonDemo3Kt? = null
            get() {
                if (field == null) {
                    field = SingletonDemo3Kt()
                }
                return field
            }
        @Synchronized
        fun getInstance() = mInstance!!
    }
    fun show() {
        println("SingletonDemo3Kt show")
    }
}

fun main() {
    SingletonDemo3Kt.getInstance().show()
}


/**
 * 背后逻辑代码:
    public final class SingletonDemo2Kt {
        private static SingletonDemo2Kt mInstance;
        public static final SingletonDemo2Kt.Companion Companion = new SingletonDemo2Kt.Companion((DefaultConstructorMarker)null);

        public final void show() {
            String var1 = "SingletonDemo2Kt show";
            boolean var2 = false;
            System.out.println(var1);
        }
        ...
        public static final class Companion {
            private final SingletonDemo2Kt getMInstance() {
                if (SingletonDemo2Kt.mInstance == null) {
                    SingletonDemo2Kt.mInstance = new SingletonDemo2Kt();
                }

            return SingletonDemo2Kt.mInstance;
        }

        private final void setMInstance(SingletonDemo2Kt var1) {
            SingletonDemo2Kt.mInstance = var1;
        }

        @NotNull
        public final SingletonDemo2Kt getInstance() {
            SingletonDemo2Kt var10000 = ((SingletonDemo2Kt.Companion)this).getMInstance();
            if (var10000 == null) {
                Intrinsics.throwNpe();
            }

            return var10000;
        }

        private Companion() {
        }

        // $FF: synthetic method
        public Companion(DefaultConstructorMarker $constructor_marker) {
            this();
        }
        }
    }
  */

Java版本

public class SingletonDemo3 {
    private static SingletonDemo3 mInstance;
    private SingletonDemo3() {}

    public static synchronized SingletonDemo3 getInstance() {
        if (mInstance == null) {
            mInstance = new SingletonDemo3();
        }
        return mInstance;
    }

    public static void show() {
        System.out.println("SingletonDemo3 show");
    }

    public static void main(String[] args) {
        SingletonDemo3.getInstance().show();
    }
}

四、双重校验DCL 的实现

Kotlin实现

class SingletonDemo4Kt private constructor() {
    companion object {
        val mInstance : SingletonDemo4Kt by lazy (mode = LazyThreadSafetyMode.SYNCHRONIZED) { SingletonDemo4Kt() }
    }

    fun show() {
        println("SingletonDemo4Kt show")
    }
}
fun main() {
    SingletonDemo4Kt.mInstance.show()
}

Java实现

public class SingletonDemo4 {
    private volatile static SingletonDemo4 mInstance;

    private SingletonDemo4() {}

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

    public void show() {
        System.out.println("show");
    }

    public static void main(String[] args) {
        SingletonDemo4.getInstance().show();
    }
}
相关推荐
居居飒17 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
刘争Stanley2 天前
如何高效调试复杂布局?Layout Inspector 的 Toggle Deep Inspect 完全解析
android·kotlin·android 15·黑屏闪屏白屏
sickworm陈浩2 天前
Java 转 Kotlin 系列:究竟该不该用 lateinit?
android·kotlin
droidHZ3 天前
Compose Multiplatform 之旅—声明式UI
android·kotlin
zhangphil3 天前
Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆角矩形实现,Kotlin(1)
android·kotlin
alexhilton6 天前
Android技巧:学习使用GridLayout
android·kotlin·android jetpack
zhangphil6 天前
Android使用PorterDuffXfermode的模式PorterDuff.Mode.SRC_OUT实现橡皮擦,Kotlin(1)
android·kotlin
IH_LZH7 天前
OkHttp源码分析:分发器任务调配,拦截器责任链设计,连接池socket复用
android·java·okhttp·kotlin
casual_clover8 天前
Android之RecyclerView显示数据列表和网格
android·kotlin
氤氲息9 天前
导入kotlin
android·开发语言·kotlin