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();
    }
}
相关推荐
wk灬丨1 小时前
Android Kotlin Flow 冷流 热流
android·kotlin·flow
晨曦_子画1 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
大福是小强1 小时前
005-Kotlin界面开发之程序猿初试Composable
kotlin·界面开发·桌面应用·compose·jetpack·可组合
&岁月不待人&4 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
小白学大数据7 小时前
正则表达式在Kotlin中的应用:提取图片链接
开发语言·python·selenium·正则表达式·kotlin
bytebeats2 天前
Kotlin 注解全面指北
android·java·kotlin
jzlhll1232 天前
kotlin android Handler removeCallbacks runnable不生效的一种可能
android·开发语言·kotlin
&岁月不待人&2 天前
Kotlin 协程使用及其详解
开发语言·kotlin
苏柘_level62 天前
【Kotlin】 基础语法笔记
开发语言·笔记·kotlin
大福是小强2 天前
002-Kotlin界面开发之Kotlin旋风之旅
kotlin·函数式编程·lambda·语法·运算符重载·扩展函数