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();
    }
}
相关推荐
xvch4 小时前
Kotlin 2.1.0 入门教程(五)
android·kotlin
xvch8 小时前
Kotlin 2.1.0 入门教程(七)
android·kotlin
zhangphil10 小时前
Android BitmapShader简洁实现马赛克,Kotlin(一)
android·kotlin
五味香18 小时前
Java学习,查找List最大最小值
android·java·开发语言·python·学习·golang·kotlin
五味香2 天前
Java学习,List截取
android·java·开发语言·python·学习·golang·kotlin
xvch2 天前
Kotlin 2.1.0 入门教程(三)
android·kotlin
小李飞飞砖2 天前
kotlin的协程的基础概念
开发语言·前端·kotlin
深色風信子3 天前
Kotlin Bytedeco OpenCV 图像图像49 仿射变换 图像裁剪
opencv·kotlin·javacpp·bytedeco·仿射变换 图像裁剪
五味香3 天前
Java学习,List移动元素
android·java·开发语言·python·学习·kotlin·list
LuckyLay3 天前
Golang学习笔记_27——单例模式
笔记·学习·golang·单例·singleton