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种单例模式

相关推荐
m0_7482451717 分钟前
Web第一次作业
java
小码的头发丝、17 分钟前
Java进阶学习笔记|面向对象
java·笔记·学习
m0_5485147721 分钟前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
坊钰1 小时前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
chenziang11 小时前
leetcode hot100 LRU缓存
java·开发语言
会说法语的猪1 小时前
springboot实现图片上传、下载功能
java·spring boot·后端
码农老起1 小时前
IntelliJ IDEA 基本使用教程及Spring Boot项目搭建实战
java·ide·intellij-idea
m0_748239831 小时前
基于web的音乐网站(Java+SpringBoot+Mysql)
java·前端·spring boot
时雨h1 小时前
RuoYi-ue前端分离版部署流程
java·开发语言·前端