/**
* 饿汉单例模式
*/
public class EagerSingleton {
private static EagerSingleton eagerSingleton = new EagerSingleton();
private EagerSingleton(){
}
public static EagerSingleton getInstance(){
return eagerSingleton;
}
/**
* 懒汉汉单例模式
*/
public class LazySingleton {
private static LazySingleton lazySingleton = null;
private LazySingleton(){
}
public static LazySingleton getInstance(){
if(lazySingleton==null)
lazySingleton = new LazySingleton();
return lazySingleton;
}
单例模式(饿汉和懒汉)
最新推荐文章于 2024-07-23 16:25:30 发布