直接上代码
/**
* @author: QXY
* @classDescribe: 单例模式[静态内部类的实现方式]
* @createTime: 2019/3/8
* @version: 1.0
*/
public class SingletomDemo implements Serializable {
private static class SingetomBulid{
private static final SingletomDemo singletomDemo=new SingletomDemo();
}
private SingletomDemo() {
if(null!=SingetomBulid.singletomDemo){
throw new RuntimeException("防止反射取多个对像");
}
}
public static SingletomDemo getInstance(){
return SingetomBulid.singletomDemo;
}
/**
* 防止反序列化生成多个对像
*/
private Object readResolve() throws ObjectStreamException{
return SingetomBulid.singletomDemo;
}
}