Java 单例模式(Singleton) 博客分类: Java 单例设计模式
程序员文章站
2024-03-24 14:34:46
...
/** * @author BestUpon * @email bestupon@foxmail.com * @date 2010-6-13上午11:34:27 * @ask 饿汉式单利模式 * @answer */ public class HungerSingleton { /** * 一开始就初始化好了实例 */ private static HungerSingleton instance = new HungerSingleton(); private HungerSingleton() { } public static HungerSingleton getInstance() { return instance; } }
/** * * @author BestUpon * @email bestupon@foxmail.com * @date 2010-6-13上午11:41:22 * @ask 懒汉式单例模式 * @answer */ public class LazySingleton { private static LazySingleton instance = null; private LazySingleton() { } public static LazySingleton getInstance() { if(instance == null){ instance = new LazySingleton(); } return instance; } }