目录
一、饿汉式的实现
Kotlin版本
object SingletonDemoKt
/**
* 背后的逻辑代码:
public final class SingletonDemoKt {
public static final SingletonDemoKt INSTANCE;
private SingletonDemoKt() {
}
static {
SingletonDemoKt var0 = new SingletonDemoKt();
INSTANCE = var0;
}
}
*/
Java版本
public class SingletonDemo {
private static SingletonDemo mInstance = new SingletonDemo();
private SingletonDemo() {}
public static SingletonDemo getInstance() {
return mInstance;
}
}
二、懒汉式的实现
Kotlin版本
class SingletonDemo2Kt {
companion object {
private var mInstance : SingletonDemo2Kt? = null
get() {
if (field == null) {
field = SingletonDemo2Kt()
}
return field
}
fun getInstance() = mInstance!!
}
fun show() {
println("SingletonDemo2Kt show")
}
}
fun main() {
SingletonDemo2Kt.getInstance().show()
}
/**
* 背后逻辑代码:
public final class SingletonDemo2Kt {
private static SingletonDemo2Kt mInstance;
public static final SingletonDemo2Kt.Companion Companion = new SingletonDemo2Kt.Companion((DefaultConstructorMarker)null);
public final void show() {
String var1 = "SingletonDemo2Kt show";
boolean var2 = false;
System.out.println(var1);
}
...
public static final class Companion {
private final SingletonDemo2Kt getMInstance() {
if (SingletonDemo2Kt.mInstance == null) {
SingletonDemo2Kt.mInstance = new SingletonDemo2Kt();
}
return SingletonDemo2Kt.mInstance;
}
private final void setMInstance(SingletonDemo2Kt var1) {
SingletonDemo2Kt.mInstance = var1;
}
@NotNull
public final SingletonDemo2Kt getInstance() {
SingletonDemo2Kt var10000 = ((SingletonDemo2Kt.Companion)this).getMInstance();
if (var10000 == null) {
Intrinsics.throwNpe();
}
return var10000;
}
private Companion() {
}
// $FF: synthetic method
public Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
}
}
*/
Java版本
public class SingletonDemo2 {
private static SingletonDemo2 mInstance;
private SingletonDemo2() {}
public static SingletonDemo2 getInstance() {
if (mInstance == null) {
mInstance = new SingletonDemo2();
}
return mInstance;
}
public static void show() {
System.out.println("SingletonDemo2 show");
}
public static void main(String[] args) {
SingletonDemo2.getInstance().show();
}
}
三、安全 懒汉式的实现
Kotlin实现
class SingletonDemo3Kt {
companion object {
private var mInstance : SingletonDemo3Kt? = null
get() {
if (field == null) {
field = SingletonDemo3Kt()
}
return field
}
@Synchronized
fun getInstance() = mInstance!!
}
fun show() {
println("SingletonDemo3Kt show")
}
}
fun main() {
SingletonDemo3Kt.getInstance().show()
}
/**
* 背后逻辑代码:
public final class SingletonDemo2Kt {
private static SingletonDemo2Kt mInstance;
public static final SingletonDemo2Kt.Companion Companion = new SingletonDemo2Kt.Companion((DefaultConstructorMarker)null);
public final void show() {
String var1 = "SingletonDemo2Kt show";
boolean var2 = false;
System.out.println(var1);
}
...
public static final class Companion {
private final SingletonDemo2Kt getMInstance() {
if (SingletonDemo2Kt.mInstance == null) {
SingletonDemo2Kt.mInstance = new SingletonDemo2Kt();
}
return SingletonDemo2Kt.mInstance;
}
private final void setMInstance(SingletonDemo2Kt var1) {
SingletonDemo2Kt.mInstance = var1;
}
@NotNull
public final SingletonDemo2Kt getInstance() {
SingletonDemo2Kt var10000 = ((SingletonDemo2Kt.Companion)this).getMInstance();
if (var10000 == null) {
Intrinsics.throwNpe();
}
return var10000;
}
private Companion() {
}
// $FF: synthetic method
public Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
}
}
*/
Java版本
public class SingletonDemo3 {
private static SingletonDemo3 mInstance;
private SingletonDemo3() {}
public static synchronized SingletonDemo3 getInstance() {
if (mInstance == null) {
mInstance = new SingletonDemo3();
}
return mInstance;
}
public static void show() {
System.out.println("SingletonDemo3 show");
}
public static void main(String[] args) {
SingletonDemo3.getInstance().show();
}
}
四、双重校验DCL 的实现
Kotlin实现
class SingletonDemo4Kt private constructor() {
companion object {
val mInstance : SingletonDemo4Kt by lazy (mode = LazyThreadSafetyMode.SYNCHRONIZED) { SingletonDemo4Kt() }
}
fun show() {
println("SingletonDemo4Kt show")
}
}
fun main() {
SingletonDemo4Kt.mInstance.show()
}
Java实现
public class SingletonDemo4 {
private volatile static SingletonDemo4 mInstance;
private SingletonDemo4() {}
public static SingletonDemo4 getInstance() {
if (mInstance == null) {
synchronized (SingletonDemo4.class) {
if (mInstance == null) {
mInstance = new SingletonDemo4();
}
}
}
return mInstance;
}
public void show() {
System.out.println("show");
}
public static void main(String[] args) {
SingletonDemo4.getInstance().show();
}
}