单例模式怕是很多程序员使用最多的设计模式了,面向对象的23种设计模式点这里。废话不多说,直接上代码:
public class Singleton { //1.懒汉型
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}