博客
关于我
如何破坏单例?我说了好几种方式,面试官:没想到你真会
阅读量:78 次
发布时间:2019-02-25

本文共 2362 字,大约阅读时间需要 7 分钟。

?????Singleton Pattern????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????

  • ????

    ?????????????????????????????????????????????????????????????????????????????????????????????????????

    import java.lang.reflect.Constructor;public class SingletonTest {    public static void main(String[] args) {        Singleton singleton = Singleton.getSingleton();        try {            Class
    singletonClass = Singleton.class; Constructor
    constructor = singletonClass.getDeclaredConstructor(null); constructor.setAccessible(true); Singleton singletonByReflect = constructor.newInstance(); System.out.println("singleton : " + singleton); System.out.println("singletonByReflect : " + singletonByReflect); System.out.println("singleton == singletonByReflect : " + (singleton == singletonByReflect)); } catch (Exception e) { e.printStackTrace(); } }}

    ???????????????????????????????????????

  • ?????

    ????????????????????????????????????????????????????????????

    public class SingletonTest {    public static void main(String[] args) {        Singleton singleton = Singleton.getSingleton();        try {            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempFile"));            oos.writeObject(singleton);            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("tempFile"));            Singleton singletonBySerialize = (Singleton) ois.readObject();            System.out.println("singleton : " + singleton);            System.out.println("singletonBySerialize : " + singletonBySerialize);            System.out.println("singleton == singletonBySerialize : " + (singleton == singletonBySerialize));        } catch (Exception e) {            e.printStackTrace();        }    }}

    ?????????????????????????????????????

  • ????????

  • ??????????

    ??????????????????????????????????????????

    private Singleton() {    if (singleton != null) {        throw new RuntimeException("Singleton constructor is called...");    }}

    ???????????????????????????????

  • ?? readResolve ??

    ??????? readResolve ????????????????????????

    private Object readResolve() {    return getSingleton();}

    ????????????? readResolve ???????????????????????????

  • ????????????????????????????????????????????????

    转载地址:http://cns.baihongyu.com/

    你可能感兴趣的文章
    postman做接口测试
    查看>>
    QT实现TCPServer连接多个客户端(附完整源码)
    查看>>
    Postman做接口测试:如何自动校验接口响应
    查看>>
    postman做接口自动化测试
    查看>>
    postman做接口自动化测试
    查看>>
    Postman入门到入土
    查看>>
    Postman入门到精通01
    查看>>
    postman入门基础 —— 接口测试流程
    查看>>
    QLineEdit 设置 科学计算法
    查看>>
    postman启动vcruntime140_1.dll未找到解决办法
    查看>>
    Postman和JMeter怎么选?区别都你总结出来了
    查看>>
    Postman和Jmeter的区别
    查看>>
    Postman如何做接口测试:如何导入 swagger 接口文档
    查看>>
    Postman如何做接口测试:如何导入 swagger 接口文档
    查看>>
    Qlik助力新西兰最大私人医院提高病患护理水平
    查看>>
    Postman如何生成接口文档
    查看>>
    Postman学习之常用断言
    查看>>
    postman导入curl内容
    查看>>
    postman居然是用electron开发的,我们来认识下这个框架吧
    查看>>
    postman常用公共函数
    查看>>