• Java 和 Kotlin 单例模式写法对比


    目录

    1、饿汉模式

    Java 写法:

    Kotlin 写法:

    Kotlin 这段代码反编译&简化后如下:

    2、懒汉模式,静态同步方法

    Java 写法:

    Kotlin 写法:

    Kotlin 这段代码反编译&简化后如下:

    3、懒汉模式,双重检查 + 同步代码块

    Java 写法:

    Kotlin 写法:

    Kotlin 这段代码反编译&简化后如下:

    4、枚举方式

    Java 写法:

    Kotlin 写法:

    5、静态内部类 + 静态代码块

    Java 写法:

    Kotlin 写法之一:

    Kotlin 这段代码反编译&简化后 如下:

    6、其他写法

    Java 的一种写法:

    Kotlin 的一种写法:


    1、饿汉模式

    Java 写法:

    1. public class SingletonDemo1 {
    2. private static SingletonDemo1 INSTANCE = new SingletonDemo1();
    3. private SingletonDemo1() {
    4. }
    5. public static SingletonDemo1 getInstance() {
    6. return INSTANCE;
    7. }
    8. }

    Kotlin 写法:

    1. object SingletonDemo1 {
    2. }

    Kotlin 这段代码反编译&简化后如下:

    1. public final class SingletonDemo1 {
    2. @NotNull
    3. public static final SingletonDemo1 INSTANCE = new SingletonDemo1();
    4. private SingletonDemo1() {
    5. }
    6. }

    2、懒汉模式,静态同步方法

    Java 写法:

    1. public class SingletonDemo2 {
    2. private volatile static SingletonDemo2 INSTANCE;
    3. private SingletonDemo2() {
    4. }
    5. public synchronized static SingletonDemo2 getInstance() {
    6. if (INSTANCE == null) {
    7. INSTANCE = new SingletonDemo2();
    8. }
    9. return INSTANCE;
    10. }
    11. }

    Kotlin 写法:

    1. class SingletonDemo2 private constructor() {
    2. companion object {
    3. private var INSTANCE: SingletonDemo2? = null
    4. get() {
    5. if (field == null) {
    6. field = SingletonDemo2()
    7. }
    8. return field
    9. }
    10. @Synchronized
    11. fun get(): SingletonDemo2 {
    12. return INSTANCE!!
    13. }
    14. }
    15. }

    Kotlin 这段代码反编译&简化后如下:

    1. public final class SingletonDemo2 {
    2. @NotNull
    3. public static final Companion Companion = new Companion();
    4. private static SingletonDemo2 INSTANCE;
    5. private SingletonDemo2() {
    6. }
    7. public static final class Companion {
    8. private Companion() {
    9. }
    10. @NotNull
    11. public final synchronized SingletonDemo2 get() {
    12. if (SingletonDemo2.INSTANCE == null) {
    13. SingletonDemo2.INSTANCE = new SingletonDemo2();
    14. }
    15. return SingletonDemo2.INSTANCE;
    16. }
    17. }
    18. public static void main(String[] args) {
    19. //调用方式
    20. SingletonDemo2 sd = SingletonDemo2.Companion.get();
    21. }
    22. }

    3、懒汉模式,双重检查 + 同步代码块

    Java 写法:

    1. public class SingletonDemo3 {
    2. private volatile static SingletonDemo3 INSTANCE;
    3. private SingletonDemo3() {
    4. }
    5. public static SingletonDemo3 getInstance() {
    6. if (INSTANCE == null) {
    7. synchronized (SingletonDemo3.class) {
    8. if (INSTANCE == null) {
    9. INSTANCE = new SingletonDemo3();
    10. }
    11. }
    12. }
    13. return INSTANCE;
    14. }
    15. }

    Kotlin 写法:

    这段代码涉及到高阶函数和属性委托。

    Lazy 内部实现类中包含了双重判断和同步锁代码块。

    1. class SingletonDemo3 private constructor() {
    2. companion object {
    3. val INSTANCE: SingletonDemo3 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    4. SingletonDemo3()
    5. }
    6. }
    7. }

    Kotlin 这段代码反编译&简化后如下:

    1. public final class SingletonDemo2 {
    2. @NotNull
    3. public static final SingletonDemo2.Companion Companion = new SingletonDemo2.Companion();
    4. @NotNull
    5. private static final Lazy INSTANCE$delegate;
    6. private SingletonDemo2() {
    7. }
    8. static {
    9. INSTANCE$delegate = LazyKt.lazy(LazyThreadSafetyMode.SYNCHRONIZED, (Function0)null.INSTANCE);
    10. }
    11. public static final class Companion {
    12. private Companion() {
    13. }
    14. @NotNull
    15. public final SingletonDemo2 getINSTANCE() {
    16. Lazy var1 = SingletonDemo2.INSTANCE$delegate;
    17. Object var2 = null;
    18. return (SingletonDemo2)var1.getValue();
    19. }
    20. }
    21. }

    4、枚举方式

    Java 写法:

    1. public enum SingletonDemo4 {
    2. INSTANCE;
    3. public void otherMethod() {
    4. }
    5. }

    Kotlin 写法:

    1. enum class SingletonDemo4 {
    2. INSTANCE;
    3. fun otherMethod() {
    4. }
    5. }

    枚举就不反编译了,没什么区别。

    5、静态内部类 + 静态代码块

    Java 写法:

    1. public class SingletonDemo5 {
    2. private static class SingletonHolder {
    3. private static final SingletonDemo5 INSTANCE = new SingletonDemo5();
    4. }
    5. private SingletonDemo5() {
    6. }
    7. public static final SingletonDemo5 getInstance() {
    8. return SingletonHolder.INSTANCE;
    9. }
    10. }

    Kotlin 写法之一:

    1. class SingletonDemo5 private constructor() {
    2. companion object {
    3. val INSTANCE = SingletonHolder.holder
    4. }
    5. private object SingletonHolder {
    6. val holder = SingletonDemo5()
    7. }
    8. }

    Kotlin 这段代码反编译&简化后 如下:

    1. public final class SingletonDemo100 {
    2. @NotNull
    3. public static final SingletonDemo100.Companion Companion = new SingletonDemo100.Companion();
    4. @NotNull
    5. private static final SingletonDemo100 INSTANCE;
    6. private SingletonDemo100() {
    7. }
    8. static {
    9. INSTANCE = SingletonDemo100.SingletonHolder.INSTANCE.getHolder();
    10. }
    11. public static final class Companion {
    12. private Companion() {
    13. }
    14. @NotNull
    15. public final SingletonDemo100 getINSTANCE() {
    16. return SingletonDemo100.INSTANCE;
    17. }
    18. }
    19. private static final class SingletonHolder {
    20. @NotNull
    21. public static final SingletonDemo100.SingletonHolder INSTANCE = new SingletonDemo100.SingletonHolder();
    22. @NotNull
    23. private static final SingletonDemo100 holder = new SingletonDemo100();
    24. @NotNull
    25. public final SingletonDemo100 getHolder() {
    26. return holder;
    27. }
    28. }
    29. }

    6、其他写法

    Java 的一种写法:

    通过静态代码块方式,借助虚拟机在类加载时创建对象。

    1. public class SingletonDemo6 {
    2. private volatile static SingletonDemo6 INSTANCE;
    3. static {
    4. INSTANCE = new SingletonDemo6();
    5. }
    6. private SingletonDemo6() {
    7. }
    8. public static SingletonDemo6 getInstance() {
    9. return INSTANCE;
    10. }
    11. }

    Kotlin 的一种写法:

    网上看别人写的,有创意,但是也很奇怪,无法完整反编译。

    1. class SingletonDemo10 private constructor(private val property: Int) {
    2. companion object {
    3. @Volatile
    4. private var INSTANCE: SingletonDemo10? = null
    5. fun getInstance(property: Int): SingletonDemo10 {
    6. return INSTANCE ?: synchronized(this) {
    7. INSTANCE ?: SingletonDemo10(property).also { INSTANCE = it }
    8. }
    9. }
    10. }
    11. }

  • 相关阅读:
    DeepSpeed框架:1-大纲和资料梳理
    已解决:树莓派外接硬盘 usb 或者sata 导致wifi无法链接 无线网卡无法使用问题
    JavaScript不会?25分钟带你上手JavaScript ES5-ES6
    Ubuntu中USB端口与外设绑定,ROS读取IMU模块数据
    kong安装及使用
    PAT乙级 1011 A+B 和 C
    2022最新版-李宏毅机器学习深度学习课程-P26 Recurrent Neural Network
    【牛客 - 剑指offer】JZ78 把二叉树打印成多行 两种方案(递归+非递归) Java实现
    100天掌握网络安全知识点!
    IDEA集成Git
  • 原文地址:https://blog.csdn.net/fesdgasdgasdg/article/details/139886966