SINGLETON单例模式 : (1) Eager 饿汉模式 : 仅适用于 Java ; public class EagerSingleton { //类被加载时,静态变量就被初始化 private static EagerSingleton ourInstance = new EagerSingleton(); /** *//** * 外界只能通过此方法获得自身的实例 * @return SingletonDemo */ public static EagerSingleton getInstance() { return ourInstance; } /** *//** * 构造函数对外不可见 * 单例模式最显著的特点 */ private EagerSingleton() { }} (2) Lazy 懒汉模式 : 适用于Java,C++ (因为static 代码块的执行顺序c++不固定,java是固定的,在构造方法之前) public class LazySingleton { //类被加载时,静态变量不会被初始化 private static LazySingleton lazySingleton = null; /** *//** * 默认构造函数 是 private * 防止外界调用,同时此类也不能被继承 */ private LazySingleton(){ } /** *//** * synchronized :同步化 * @return */ synchronized public static LazySingleton getInstance(){ if(lazySingleton == null){ lazySingleton = new LazySingleton(); } return lazySingleton; }}