当前位置: 首页 > news >正文

网站建设策划方案模板大连网站建设短期培训班

网站建设策划方案模板,大连网站建设短期培训班,装饰工程公司,a5wordpress主题目录 1. 序列化和反序列化2. 反射 这里单例模式就不多说了 23种设计模式之单例模式 1. 序列化和反序列化 这里用饿汉式来做例子 LazySingleton import java.io.Serializable;public class LazySingleton implements Serializable {private static LazySingleton lazySinglet…

目录

  • 1. 序列化和反序列化
  • 2. 反射

这里单例模式就不多说了
23种设计模式之单例模式

1. 序列化和反序列化

这里用饿汉式来做例子
LazySingleton

import java.io.Serializable;public class LazySingleton implements Serializable {private static LazySingleton lazySingleton = null;private LazySingleton() {}public static synchronized LazySingleton getInstance() {if (lazySingleton == null) {lazySingleton = new LazySingleton();}return lazySingleton;}
}

TestSerializer

import java.io.*;public class TestSerializer {public static void main(String[] args) throws IOException, ClassNotFoundException {// 懒汉式LazySingleton instance = LazySingleton.getInstance();// 通过序列化和反序列化的方式,创建对象ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton"));oos.writeObject(instance);ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("singleton")));LazySingleton objInstance = (LazySingleton)ois.readObject();System.out.println(instance);System.out.println(objInstance);}
}

输出结果:

yxz.singleton.LazySingleton@3764951d
yxz.singleton.LazySingleton@312b1dae

可以看到这俩对象不一样。
但是使用枚举输出结果是一样的。

如果不让其破坏呢???

我们在单例中添加一个方法。

import java.io.Serializable;public class LazySingleton implements Serializable {private static LazySingleton lazySingleton = null;private LazySingleton() {}public static synchronized LazySingleton getInstance() {if (lazySingleton == null) {lazySingleton = new LazySingleton();}return lazySingleton;}private Object readResolve() {return lazySingleton;}
}

输出结果:

yxz.singleton.LazySingleton@3764951d
yxz.singleton.LazySingleton@3764951d

这样输出结果是一样的了。因为反序列化创建对象时,是通过反射创建的,反射会调用我们自己的readResolve方法,如果重写,会调用这个,否则会破坏单例模式。

2. 反射

通过字节码对象创建构造器对象,通过构造器对象,初始化单例对象,由于单例对象的构造方法是私有化的,调用构造器中的方法,赋予权限,创建单例对象。

TestReflect

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class TestReflect {public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {Class clz = LazySingleton.class;// 通过字节码对象创建构造器对象Constructor constructor = clz.getDeclaredConstructor();// 赋予权限constructor.setAccessible(true);// 初始化单例对象LazySingleton clzInstance = (LazySingleton)constructor.newInstance();System.out.println(clzInstance);LazySingleton instance = LazySingleton.getInstance();System.out.println(instance);}
}

输出结果:

yxz.singleton.LazySingleton@1b6d3586
yxz.singleton.LazySingleton@4554617c

坏了,又是一样的,这可怎么办!!!
我们如何阻止呢?
我们对代码的构造模式进行修改

import java.io.Serializable;public class LazySingleton implements Serializable {private static LazySingleton lazySingleton = null;private LazySingleton() {if(lazySingleton != null){throw new RuntimeException("不允许创建多个实例");}}public static synchronized LazySingleton getInstance() {if (lazySingleton == null) {lazySingleton = new LazySingleton();}return lazySingleton;}private Object readResolve() {return lazySingleton;}
}

这样我们再运行,输出结果是:

yxz.singleton.LazySingleton@1b6d3586
yxz.singleton.LazySingleton@4554617c

恭喜你,仍然能出现两个!!!但是,我们先创建一个对象,再使用反射呢?

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class TestReflect {public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {LazySingleton instance = LazySingleton.getInstance();System.out.println(instance);Class clz = LazySingleton.class;Constructor constructor = clz.getDeclaredConstructor();constructor.setAccessible(true);LazySingleton clzInstance = (LazySingleton)constructor.newInstance();System.out.println(clzInstance);}
}

运行结果:

yxz.singleton.LazySingleton@1b6d3586
Exception in thread "main" java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at yxz.singleton.broker.TestReflect.main(TestReflect.java:16)
Caused by: java.lang.RuntimeException: 不允许创建多个实例at yxz.singleton.LazySingleton.<init>(LazySingleton.java:10)... 5 more

这才没有创建多个。

当然,我建议是使用枚举来组织它,不过,我在使用的时候,还是没怎怎么故意使用反射或者序列化这些来破坏单例模式。

http://www.yayakq.cn/news/870252/

相关文章:

  • 百度商桥 手机网站禁用wordpress编辑器
  • 义乌创源网站建设wordpress编辑器自定义按钮
  • 设计师接单的十个网站软件开发可以做网站么
  • 怎样做化妆品公司网站厦门网站建设及维护
  • 车陂手机网站开发成都高投建设开发有限公司网站
  • 网络公司构建网站网站设计实训心得
  • 英文旅游网站建设wordpress add_options_page
  • 辽阳网站建设多少钱四川建设厅证件查询网站
  • 建设网站女装名字大全微信商城网站
  • 开发网站需要多少资金河池网站优化
  • 亳州市建设局网站在线 crm
  • 百度网站权重排名软件开发方案模板
  • 网站制作教程书籍互联网发展趋势
  • 企业网站建设存在的不足南京制作网页学校
  • 海南省两学一做网站杭州游戏软件开发公司
  • 郑州网站设计报价表网站怎么营销
  • 学前端要逛那些网站wordpress 外贸站主题
  • 电商网站设计网络服务泰安市建设职工培训中心网站官网
  • 做机械比较好的外贸网站深圳外贸公司联系电话
  • 北京网站定制设计开发公司大城网站优化
  • 绵阳定制网站建设面料做电商 哪个网站好
  • 聊城做网站的公司流程找人做网站服务器不是自己的怎么办
  • 石景山做网站公司建设一个网站需要考虑什么
  • ui个人作品集网站办公用品网站建设可行性分析
  • 自动发卡网站开发网络课程网站建设
  • 天水市建设路第二小学网站营子区住房和城乡建设局网站
  • 地产网站建设公司谷歌seo视频教程
  • 怎么样才算是一个网站页面简单网站开发准备
  • 用wordpress做答题网站营销型网站是啥意思
  • 千博企业网站管理系统完整版 2014百度官方网站怎么做