• kotlin实现java的单例模式


    代码

    package com.flannery.interviewdemo.singleinstance
    
    //https://blog.csdn.net/Jason_Lee155/article/details/128796742
    Java实现
    //public class SingletonDemo {
    //    private static SingletonDemo instance=new SingletonDemo();
    //    private SingletonDemo()
    //    {
    //
    //    }
    //    public static SingletonDemo getInstance()
    //    {
    //        return instance;
    //    }
    //}
    // 饿汉式
    object SingleInstance1
    
    // 懒汉式
    Java实现
    //public class SingletonDemo {
    //    private static SingletonDemo instance;
    //    private SingletonDemo(){}
    //    public static SingletonDemo getInstance(){
    //        if(instance==null){
    //            instance=new SingletonDemo();
    //        }
    //        return instance;
    //    }
    //}
    //
    class SingletonDemo2 private constructor() {
        companion object {
            private var instance: SingletonDemo2? = null
                get() {
                    if (field == null) {
                        field = SingletonDemo2()
                    }
                    return field
                }
    
            fun get(): SingletonDemo2 {
                //细心的小伙伴肯定发现了,这里不用getInstance作为为方法名,是因为在伴生对象声明时,内部已有getInstance方法,所以只能取其他名字
                return instance!!
            }
        }
    }
    
    // 线程安全的懒汉式
    //public class SingletonDemo {
    //    private static SingletonDemo instance;
    //    private SingletonDemo() {}
    //    public static synchronized SingletonDemo getInstance() {//使用同步锁
    //        if (instance == null) {
    //            instance = new SingletonDemo ();
    //        }
    //        return instance;
    //    }
    //}
    class SingletonDemo3 private constructor() {
        companion object {
            private var instance: SingletonDemo3? = null
                get() {
                    if (field == null) {
                        field = SingletonDemo3()
                    }
                    return field
                }
    
            @Synchronized
            fun get(): SingletonDemo3 {
                return instance!!
            }
        }
    
    }
    
    // 双重校验锁
    Java实现
    //public class SingletonDemo {
    //    private volatile static SingletonDemo instance;
    //    private SingletonDemo(){}
    //    public static SingletonDemo getInstance(){
    //        if(instance==null){
    //            synchronized (SingletonDemo.class){
    //                if(instance==null){
    //                    instance=new SingletonDemo();
    //                }
    //            }
    //        }
    //        return instance;
    //    }
    //}
    //
    //kotlin实现
    class SingletonDemo4 private constructor() {
        companion object {
            val instance: SingletonDemo4 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
                SingletonDemo4()
            }
        }
    }
    
    // 静态内部类
    //Java实现
    //public class SingletonDemo {
    //    private static class SingletonHolder{
    //        private static SingletonDemo instance=new SingletonDemo();
    //    }
    //    private SingletonDemo(){
    //        System.out.println("Singleton has loaded");
    //    }
    //    public static SingletonDemo getInstance(){
    //        return SingletonHolder.instance;
    //    }
    //}
    //kotlin实现
    class SingletonDemo5 private constructor() {
        companion object {
            val instance = SingletonHolder.holder
        }
    
        private object SingletonHolder {
            val holder= SingletonDemo5()
        }
    }
    
    // Double check
    class SingletonDemo6 private constructor(private val property: Int) {
        //这里参数可以根据实际需求发生改变
        companion object {
            @Volatile private var instance: SingletonDemo6? = null
            fun getInstance(property: Int) =
                instance ?: synchronized(this) {
                    instance ?: SingletonDemo6(property).also { instance = it }
                }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    java实现的双重校验反编译成smali

    .class public LA001单例模式/DoubleCheckSingleInstance;
    .super Ljava/lang/Object;
    .source "DoubleCheckSingleInstance.java"
    
    
    # static fields
    .field private static volatile instance:LA001单例模式/DoubleCheckSingleInstance;
    
    
    # direct methods
    .method static constructor ()V
        .registers 1
    
        .prologue
        .line 4
        const/4 v0, 0x0
    
        sput-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
    
        return-void
    .end method
    
    .method public constructor ()V
        .registers 1
    
        .prologue
        .line 3
        invoke-direct {p0}, Ljava/lang/Object;->()V
    
        return-void
    .end method
    
    .method public static getInstance()LA001单例模式/DoubleCheckSingleInstance;
        .registers 2
    
        .prologue
        .line 7
        sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
    
        if-nez v0, :cond_13
    
        .line 8
        const-class v1, LA001单例模式/DoubleCheckSingleInstance;
    
        monitor-enter v1
    
        .line 9
        :try_start_7
        sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
    
        if-nez v0, :cond_12
    
        .line 10
        new-instance v0, LA001单例模式/DoubleCheckSingleInstance;
    
        invoke-direct {v0}, LA001单例模式/DoubleCheckSingleInstance;->()V
    
        sput-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
    
        .line 12
        :cond_12
        monitor-exit v1
        :try_end_13
        .catchall {:try_start_7 .. :try_end_13} :catchall_16
    
        .line 14
        :cond_13
        sget-object v0, LA001单例模式/DoubleCheckSingleInstance;->instance:LA001单例模式/DoubleCheckSingleInstance;
    
        return-object v0
    
        .line 12
        :catchall_16
        move-exception v0
    
        :try_start_17
        monitor-exit v1
        :try_end_18
        .catchall {:try_start_17 .. :try_end_18} :catchall_16
    
        throw v0
    .end method
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    资料

    Kotlin的5种单例模式

  • 相关阅读:
    猿创征文|栈和队列OJ刷题
    Unity --- 给物体添加重力
    kafka下载安装
    html简单案例
    Java变量的特殊传递机制和实现细节
    下载调试器 JTAG和SWD
    慧销平台ThreadPoolExecutor内存泄漏分析
    RPA常见实例
    逐鹿人形机器人,百度、腾讯、小米卷起来
    《开源软件的影响力》
  • 原文地址:https://blog.csdn.net/AdrianAndroid/article/details/132579886