kotlin实现java的单例模式

代码

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 }
            }
    }
}

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

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 <clinit>()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 <init>()V
    .registers 1

    .prologue
    .line 3
    invoke-direct {p0}, Ljava/lang/Object;-><init>()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;-><init>()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

资料

Kotlin的5种单例模式

相关推荐
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨2 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构
P.H. Infinity3 小时前
【RabbitMQ】07-业务幂等处理
java·rabbitmq·java-rabbitmq
爱吃土豆的程序员3 小时前
java XMLStreamConstants.CDATA 无法识别 <![CDATA[]]>
xml·java·cdata
2401_857610034 小时前
多维视角下的知识管理:Spring Boot应用
java·spring boot·后端