单例模式是比较简单的,想起以前面试的时候也是遇到最多的,但是要真的写好里面也很多讲究,什么饥饿、线程安全等等,参考http://www.runoob.com/design-pattern/singleton-pattern.html这里只写双重锁定DCL的单例
public class Singleton {
private volatile static Singleton instance;
private Singleton() {
super();
}
public static Singleton getInstance() {
if(instance == null) {
synchronized(Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}