标签:null 设计 nal let ati turn eager instance ret
/**
* 贪婪模式(EAGER)
*/
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
// DO NOTHING
}
public static final Singleton getInstance() {
return INSTANCE;
}
}
///////////////////////////////////////////////////////////////
/**
* 懒汉模式(LAZY)
*/
public class Singleton {
private volatile static Singleton INSTANCE = null;
private Singleton() {
// DO NOTHING
}
public static Singleton getInstance() {
if (null == INSTANCE) {
synchronized (Singleton.class) {
INSTANCE = new Singleton();
}
}
return INSTANCE;
}
}
///////////////////////////////////////////////////////////
/**
* 嵌套类
*/
public class Singleton {
private Singleton() {
// DO NOTHING
}
public static final Singleton getInstance() {
return SingletonHelper.INSTANCE;
}
private static final class SingletonHelper {
private static final Singleton INSTANCE = new Singleton();
}
}
/////////////////////////////////////////////////////////////
/**
* 枚举
*/
标签:null 设计 nal let ati turn eager instance ret
原文地址:http://www.cnblogs.com/yang-taiyue/p/6518459.html