单例模式(懒汉式 与 饿汉式)
懒汉式:
public class SingleTon{
private static SingleTon singleTon;
private SingleTon(){}
public static SingleTon getSingleTon(){
if(singleTon != null){
synchronized(SingleTon.class){
if(singleTon != null){
singleTon = new SingleTon();
}
}
}
return singleTon;
}
}
饿汉式:
public class SingleTon{
private static SingleTon singleTon = new SingleTon();
private SingleTon(){}
public static SingleTon getSingleTon(){
return singleTon;
}
}