日期:2014-05-18 浏览次数:20911 次
public class LazySingleton {
private static LazySingleton instance = null;
private LazySingleton(){};
public static synchronized LazySingleton getInstance(){
if(instance==null){
instance = new LazySingleton();
}
return instance;
}
} public class EagerSingleton {
private static EagerSingleton instance = new EagerSingleton();
private EagerSingleton(){};
public static EagerSingleton getInstance(){
return instance;
}
}
package com.javapatterns.singleton;
/**
* 饿汉式单例类
*
*/
public class EagerSingleton {
private static final EagerSingleton m_instance = new EagerSingleton();
/**
* 私有的构造方法
*/
private EagerSingleton(){
}
/**
* 功能:静态工厂方法
* @return EagerSingleton