单例模式(懒汉式)防止反射漏洞

public class SingLazy {
    //类的初始化 不初始化这个对象(延迟加载,用到再创建)
    private  static SingLazy instane;
    //私有化构造器
    private SingLazy(){
        
     //多次调用,抛出异常,防止反射
        if(instane!=null){
            throw new RuntimeException();
        }

    }

    //方法同步,销量低
    public static  synchronized SingLazy gets(){
        if(instane==null){
            instane=new SingLazy();
        }

        return instane;
    }
}

版权声明:本文为cxscxs123cxs原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。