学习单例设计模式:
public class Singleton3 {
//懒汉式
private static Singleton3 singleton3 = null;
private Singleton3() {
}
public static Singleton3 getInstance() {
if (singleton3 == null) {
singleton3=new Singleton3();
}
return singleton3;
}
}
public class Singleton2 {
//饿汉式
private static final Singleton2 singleton2=new Singleton2();
private Singleton2(){}
public static Singleton2 getInstance(){
return singleton2;
}
}
public class Singleton {
//双重锁形式
private static Singleton singleton=null;
private static Singleton getInstance(){
if(singleton==null){
synchronized(Singleton.class){
if(singleton==null){
singleton=new Singleton();
}
}
}
return singleton;
}
private Singleton(){}
}