设计模式之单例模式 发表于 2017-10-24 | 分类于 java | 单例模式是一个创建型的模式; 懒汉式(支持多线程) 123456789101112131415161718public class Singleton { private volatile static Singleton singleton; private Singleton() { } public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }} 饿汉式(支持多线程) 12345678public class Singleton { private static Singleton instance = new Singleton(); private Singleton() { } public static Singleton getInstance() { return instance; }} 1234567891011public class Singleton { private static class SingletonHolder{ private static Singleton instance = new Singleton(); } private Singleton() { } public static Singleton getInstance() { return SingletonHolder.instance; }} 如果对您有价值,看官可以打赏的!