博客
关于我
如何破坏单例?我说了好几种方式,面试官:没想到你真会
阅读量: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/

    你可能感兴趣的文章
    python file
    查看>>
    Python FileDialog获取文件夹路径不是文件
    查看>>
    python filter过滤器的使用_python基础知识分享:zip()、filter函数和reduce如何使用?...
    查看>>
    python flask 请求code 400, message Bad request version
    查看>>
    Python Flink Stateful函数入口上的Kafka键访问
    查看>>
    Python float - str - 浮动怪异
    查看>>
    Python Flower库:分布式任务管理与监控
    查看>>
    Python for 循环和迭代器行为
    查看>>
    Python For循环多次返回
    查看>>
    python frame_python3 selenium自动化 frame表单嵌套的切换方法
    查看>>
    Python ftplib - 指定端口
    查看>>
    Python furl库:一键搞定复杂URL操作
    查看>>
    Python GC 也会关闭文件吗?
    查看>>
    python gen_key.py 报错 提示找不到OpenSSL lib
    查看>>
    python getattrribute_Python学习——面向对象高级之反射
    查看>>
    Python进阶语法:字典推导式
    查看>>
    python gil_Python中GIL的使用详解
    查看>>
    python glob.glob使用
    查看>>
    python glob的安装和使用
    查看>>
    Python google drive API 下载,文件在哪里?
    查看>>