单利模式实现有饿汉式,懒汉式,双重检测等待,接下来介绍的是一个枚举类的单利,线程安全
public class SingletonDemo { // private final static class SingletonHolder { // private static final Singleton INSTANCE = new Singleton(); // } private SingletonDemo() { } public static SingletonDemo getInstance() { return Singleton.INSTANCE.getInstance(); } private enum Singleton { INSTANCE; private SingletonDemo singleton; Singleton() { singleton = new SingletonDemo(); } public SingletonDemo getInstance() { return singleton; } } }