目录
- public class SingletonDemo1 {
-
- private static SingletonDemo1 INSTANCE = new SingletonDemo1();
-
- private SingletonDemo1() {
-
- }
-
- public static SingletonDemo1 getInstance() {
- return INSTANCE;
- }
-
- }
- object SingletonDemo1 {
-
- }
- public final class SingletonDemo1 {
- @NotNull
- public static final SingletonDemo1 INSTANCE = new SingletonDemo1();
- private SingletonDemo1() {
- }
- }
- public class SingletonDemo2 {
-
- private volatile static SingletonDemo2 INSTANCE;
-
- private SingletonDemo2() {
-
- }
-
- public synchronized static SingletonDemo2 getInstance() {
- if (INSTANCE == null) {
- INSTANCE = new SingletonDemo2();
- }
- return INSTANCE;
- }
-
- }
- 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!!
- }
- }
-
- }
- 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();
-
- }
- }
- 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;
- }
-
- }
这段代码涉及到高阶函数和属性委托。
Lazy 内部实现类中包含了双重判断和同步锁代码块。
- class SingletonDemo3 private constructor() {
-
- companion object {
- val INSTANCE: SingletonDemo3 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
- SingletonDemo3()
- }
- }
-
-
- }
- 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();
- }
- }
- }
- public enum SingletonDemo4 {
- INSTANCE;
-
- public void otherMethod() {
-
- }
-
- }
- enum class SingletonDemo4 {
- INSTANCE;
-
- fun otherMethod() {
-
- }
- }
枚举就不反编译了,没什么区别。
- public class SingletonDemo5 {
-
- private static class SingletonHolder {
- private static final SingletonDemo5 INSTANCE = new SingletonDemo5();
- }
-
- private SingletonDemo5() {
- }
-
- public static final SingletonDemo5 getInstance() {
- return SingletonHolder.INSTANCE;
- }
-
- }
- class SingletonDemo5 private constructor() {
-
- companion object {
- val INSTANCE = SingletonHolder.holder
- }
-
- private object SingletonHolder {
- val holder = SingletonDemo5()
- }
-
- }
- 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;
- }
- }
- }
通过静态代码块方式,借助虚拟机在类加载时创建对象。
- public class SingletonDemo6 {
- private volatile static SingletonDemo6 INSTANCE;
-
- static {
- INSTANCE = new SingletonDemo6();
- }
-
- private SingletonDemo6() {
-
- }
-
- public static SingletonDemo6 getInstance() {
- return INSTANCE;
- }
-
- }
网上看别人写的,有创意,但是也很奇怪,无法完整反编译。
- 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 }
- }
- }
-
- }
-
- }