首页 > 编程 > Java > 正文

java中的单例模式

2019-11-08 02:30:07
字体:
来源:转载
供稿:网友

恶汉式

public class EagerSingleton {        PRivate static final EagerSingleton instance = new EagerSingleton();        private EagerSingleton() {        }        public static EagerSingleton getInstance() {          return instance;      }    }  

懒汉式

public class LazySingleton {            private static LazySingleton instance = null;            private LazySingleton() {                }            public synchronized static LazySingleton getInstance() {          if(instance == null){              instance = new LazySingleton();          }          return instance;      }    }  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表