单子模式1:
单子模式2:
public class Singleton {
//1.私有的静态类变量;
//2.私有的构造子;
//3.公有的静态工厂方法
private static Singleton instance = null;
private Singleton(){}
synchronized public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
单子模式2:
public class Singleton {
/*
1.私有的静态类变量;
2.构造子私有;
3.公有的静态工厂方法;
*/
private static Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}