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

    你可能感兴趣的文章
    Object.keys()的详解和用法
    查看>>
    objectForKey与valueForKey在NSDictionary中的差异
    查看>>
    Objective - C 小谈:消息机制的原理与使用
    查看>>
    OBJECTIVE C (XCODE) 绘图功能简介(转载)
    查看>>
    Objective-C ---JSON 解析 和 KVC
    查看>>
    Objective-C 编码规范
    查看>>
    Objective-Cfor循环实现Factorial阶乘算法 (附完整源码)
    查看>>
    Objective-C——判断对象等同性
    查看>>
    objective-c中的内存管理
    查看>>
    Objective-C之成魔之路【7-类、对象和方法】
    查看>>
    Objective-C享元模式(Flyweight)
    查看>>
    Objective-C以递归的方式实现二叉搜索树算法(附完整源码)
    查看>>
    Objective-C内存管理教程和原理剖析(三)
    查看>>
    Objective-C实现 Greedy Best First Search最佳优先搜索算法(附完整源码)
    查看>>
    Objective-C实现 jugglerSequence杂耍者序列算法 (附完整源码)
    查看>>
    Objective-C实现 lattice path格子路径算法(附完整源码)
    查看>>
    Objective-C实现1000 位斐波那契数算法(附完整源码)
    查看>>
    Objective-C实现2 个数字之间的算术几何平均值算法(附完整源码)
    查看>>
    Objective-C实现2d 表面渲染 3d 点算法(附完整源码)
    查看>>
    Objective-C实现2D变换算法(附完整源码)
    查看>>