//懒汉模式
public class Single{
private static volatile Single instance;
private Single(){
}
public static Single getInstance(){
if(instance==null){
synchronized(Single.class){
if(instance==null)
instance=new Single();
}
}
return instance;
}
}
//饿汉模式
public class Single2{
private static final Single2 INSTANCE = new Single2();
private Single2(){
}
public static Single2 getInstance(){
return INSTANCE;
}
}
//静态内部类
public class Single3{
private static class SingletonHolder{
private static final Single3 INSTANCE = new Single3();
}
private Single3(){
}
public static final Single3 getInstance(){
return SingletonHolder.INSTANCE;
}
}
//枚举
public enum Single4{
INSTANCE;
public void fun1(){
//
}
}
//使用
Single4.INSTANCE.fun1();