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

    你可能感兴趣的文章
    nodejs 运行CMD命令
    查看>>
    Nodejs+Express+Mysql实现简单用户管理增删改查
    查看>>
    nodejs+nginx获取真实ip
    查看>>
    nodejs-mime类型
    查看>>
    NodeJs——(11)控制权转移next
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    nodejs与javascript中的aes加密
    查看>>
    nodejs中Express 路由统一设置缓存的小技巧
    查看>>
    nodejs中express的使用
    查看>>
    Nodejs中的fs模块的使用
    查看>>
    NodeJS使用淘宝npm镜像站的各种姿势
    查看>>
    nodejs包管理工具对比:npm、Yarn、cnpm、npx
    查看>>
    NodeJs单元测试之 API性能测试
    查看>>
    nodejs图片转换字节保存
    查看>>
    nodejs在Liunx上的部署生产方式-PM2
    查看>>
    nodejs字符与字节之间的转换
    查看>>
    NodeJs学习笔记001--npm换源
    查看>>
    NodeJs学习笔记002--npm常用命令详解
    查看>>
    nodejs学习笔记一——nodejs安装
    查看>>