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();
    }
}
相关推荐
闲暇部落9 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
长亭外的少年1 天前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
JIAY_WX1 天前
kotlin
开发语言·kotlin
麦田里的守望者江1 天前
KMP 中的 expect 和 actual 声明
android·ios·kotlin
菠菠萝宝2 天前
【YOLOv8】安卓端部署-1-项目介绍
android·java·c++·yolo·目标检测·目标跟踪·kotlin
恋猫de小郭2 天前
Kotlin Multiplatform 未来将采用基于 JetBrains Fleet 定制的独立 IDE
开发语言·ide·kotlin
枫__________2 天前
kotlin 协程 job的cancel与cancelAndJoin区别
android·开发语言·kotlin
鸠摩智首席音效师2 天前
如何在 Ubuntu 上配置 Kotlin 应用环境 ?
linux·ubuntu·kotlin
jikuaidi6yuan4 天前
Java与Kotlin在鸿蒙中的地位
java·kotlin·harmonyos
liulanba4 天前
Kotlin的data class
前端·微信·kotlin