代码
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