目录

1、饿汉模式

Java 写法:

Kotlin 写法:

Kotlin 这段代码反编译&简化后如下:

2、懒汉模式,静态同步方法

Java 写法:

Kotlin 写法:

Kotlin 这段代码反编译&简化后如下:

3、懒汉模式,双重检查 + 同步代码块

Java 写法:

Kotlin 写法:

Kotlin 这段代码反编译&简化后如下:

4、枚举方式

Java 写法:

Kotlin 写法:

5、静态内部类 + 静态代码块

Java 写法:

Kotlin 写法之一:

Kotlin 这段代码反编译&简化后 如下:

6、其他写法

Java 的一种写法:

Kotlin 的一种写法:


1、饿汉模式

Java 写法:

public class SingletonDemo1 {

    private static SingletonDemo1 INSTANCE = new SingletonDemo1();

    private SingletonDemo1() {

    }

    public static SingletonDemo1 getInstance() {
        return INSTANCE;
    }

}

Kotlin 写法:

object SingletonDemo1 {

}

Kotlin 这段代码反编译&简化后如下:

public final class SingletonDemo1 {
   @NotNull
   public static final SingletonDemo1 INSTANCE = new SingletonDemo1();
   private SingletonDemo1() {
   }
}

2、懒汉模式,静态同步方法

Java 写法:

public class SingletonDemo2 {

    private volatile static SingletonDemo2 INSTANCE;

    private SingletonDemo2() {

    }

    public synchronized static SingletonDemo2 getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SingletonDemo2();
        }
        return INSTANCE;
    }

}

Kotlin 写法:

class SingletonDemo2 private constructor() {

    companion object {
        private var INSTANCE: SingletonDemo2? = null
            get() {
                if (field == null) {
                    field = SingletonDemo2()
                }
                return field
            }

        @Synchronized
        fun get(): SingletonDemo2 {
            return INSTANCE!!
        }
    }

}

Kotlin 这段代码反编译&简化后如下:

public final class SingletonDemo2 {
    @NotNull
    public static final Companion Companion = new Companion();
    private static SingletonDemo2 INSTANCE;

    private SingletonDemo2() {
    }

    public static final class Companion {
        private Companion() {
        }

        @NotNull
        public final synchronized SingletonDemo2 get() {
            if (SingletonDemo2.INSTANCE == null) {
                SingletonDemo2.INSTANCE = new SingletonDemo2();
            }
            return SingletonDemo2.INSTANCE;
        }

    }

    public static void main(String[] args) {
        //调用方式
        SingletonDemo2 sd = SingletonDemo2.Companion.get();

    }
}

3、懒汉模式,双重检查 + 同步代码块

Java 写法:

public class SingletonDemo3 {

    private volatile static SingletonDemo3 INSTANCE;

    private SingletonDemo3() {

    }

    public static SingletonDemo3 getInstance() {
        if (INSTANCE == null) {
            synchronized (SingletonDemo3.class) {
                if (INSTANCE == null) {
                    INSTANCE = new SingletonDemo3();
                }
            }
        }
        return INSTANCE;
    }

}

Kotlin 写法:

这段代码涉及到高阶函数和属性委托。

Lazy 内部实现类中包含了双重判断和同步锁代码块。

class SingletonDemo3 private constructor() {

    companion object {
        val INSTANCE: SingletonDemo3 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SingletonDemo3()
        }
    }


}

Kotlin 这段代码反编译&简化后如下:

public final class SingletonDemo2 {
    @NotNull
    public static final SingletonDemo2.Companion Companion = new SingletonDemo2.Companion();
    @NotNull
    private static final Lazy INSTANCE$delegate;

    private SingletonDemo2() {
    }

    static {
        INSTANCE$delegate = LazyKt.lazy(LazyThreadSafetyMode.SYNCHRONIZED, (Function0)null.INSTANCE);
    }

    public static final class Companion {
        private Companion() {
        }

        @NotNull
        public final SingletonDemo2 getINSTANCE() {
            Lazy var1 = SingletonDemo2.INSTANCE$delegate;
            Object var2 = null;
            return (SingletonDemo2)var1.getValue();
        }
    }
}

4、枚举方式

Java 写法:

public enum SingletonDemo4 {
    INSTANCE;

    public void otherMethod() {

    }

}

Kotlin 写法:

enum class SingletonDemo4 {
    INSTANCE;

    fun otherMethod() {

    }
}

枚举就不反编译了,没什么区别。

5、静态内部类 + 静态代码块

Java 写法:

public class SingletonDemo5 {

    private static class SingletonHolder {
        private static final SingletonDemo5 INSTANCE = new SingletonDemo5();
    }

    private SingletonDemo5() {
    }

    public static final SingletonDemo5 getInstance() {
        return SingletonHolder.INSTANCE;
    }

}

Kotlin 写法之一:

class SingletonDemo5 private constructor() {

    companion object {
        val INSTANCE = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder = SingletonDemo5()
    }

}

Kotlin 这段代码反编译&简化后 如下:

public final class SingletonDemo100 {
    @NotNull
    public static final SingletonDemo100.Companion Companion = new SingletonDemo100.Companion();
    @NotNull
    private static final SingletonDemo100 INSTANCE;

    private SingletonDemo100() {
    }

    static {
        INSTANCE = SingletonDemo100.SingletonHolder.INSTANCE.getHolder();
    }

    public static final class Companion {
        private Companion() {
        }

        @NotNull
        public final SingletonDemo100 getINSTANCE() {
            return SingletonDemo100.INSTANCE;
        }
    }

    private static final class SingletonHolder {
        @NotNull
        public static final SingletonDemo100.SingletonHolder INSTANCE = new SingletonDemo100.SingletonHolder();
        @NotNull
        private static final SingletonDemo100 holder = new SingletonDemo100();

        @NotNull
        public final SingletonDemo100 getHolder() {
            return holder;
        }
    }
}

6、其他写法

Java 的一种写法:

通过静态代码块方式,借助虚拟机在类加载时创建对象。

public class SingletonDemo6 {
    private volatile static SingletonDemo6 INSTANCE;

    static {
        INSTANCE = new SingletonDemo6();
    }

    private SingletonDemo6() {

    }

    public static SingletonDemo6 getInstance() {
        return INSTANCE;
    }

}

Kotlin 的一种写法:

网上看别人写的,有创意,但是也很奇怪,无法完整反编译。

class SingletonDemo10 private constructor(private val property: Int) {

    companion object {

        @Volatile
        private var INSTANCE: SingletonDemo10? = null

        fun getInstance(property: Int): SingletonDemo10 {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: SingletonDemo10(property).also { INSTANCE = it }
            }
        }

    }

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部