饿汉式
public class LazySingleton {
private volatile static LazySingleton lazySingleton;
private LazySingleton(){}
public static LazySingleton getInstance(){
if(lazySingleton == null){
synchronized (LazySingleton.class) {
if (lazySingleton == null) {
lazySingleton = new LazySingleton();
}
}
}
return lazySingleton;
}
}
懒汉式
public class HungrySingleton {
private final static HungrySingleton HUNGRY_SINGLETON;
static {
HUNGRY_SINGLETON = new HungrySingleton();
}
private HungrySingleton(){}
public static HungrySingleton getInstance(){
return HUNGRY_SINGLETON;
}
}
枚举式
public enum EnumSingleton {
INSTANCE(new Object());
private Object data;
EnumSingleton(Object data) {
this.data = data;
}
public static EnumSingleton getInstance(){
return INSTANCE;
}
}