设计模式 单例模式
//jvm管理 线程安全的 //lazy initialization holder class //内部类给单例作延时加载 public class Singleton { private static final class Holder { private static final Singleton instance = new Singleton(); } private Singleton() { } public Singleton getInstance() { return Holder.instance; } } // 枚举--单例模式 enum EnumSingleton { INSTANCE; // 枚举类型构造函数必须为 private 并且元素是静态的 定义完 枚举就可以使用 private EnumSingleton() {} public Singleton build() { final Singleton instance = new Singleton(); return instance; } } //外部调用 Singleton instance = EnumSingleton.INSTANCE.build(); // 懒汉模式 public class Singleton { // initailzed during class loading private static final Singleton INSTANCE = new Singleton(); // to prevent creating another instance of Singleton private Singleton() { } public static Singleton getSingleton() { return INSTANCE; } } //