1、懒汉模式
public class Single{
private static Single single;
private Single(){}
public static Single getSingle(){
if(single==null)
single=new Single();
return single;
}
}
2、;懒汉模式(线程安全)
public class Single{
private static Single singel;
private Single(){}
public static Synchronized Single getSingle(){
if(single==null)
single=new Single();
return single;
}
}
3、恶汉模式
public class Single{
private static Single single=new Single();
private Single();
public static Single getSingle(){
return single;
}
}
4、恶汉模式2
public class Single{
private static Single single;
private Single(){single=new Single()};
public static Single getSingle(){
return single;
}
}
5、双重检测模式
public class Single{
private static Single single;
private Single(){};
public static Single getSingle(){
if(single==null){
Synchronized (Single.class){
if(single==null)
single=new Single();
}
}return single;
}
}
6、静态内部类模式(推荐)
public class Single{
private static class SingleInner{
private static final Single single=new Single();
}
private Single(){}
private
}