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

超星网站开发实战答案深圳报业集团官网

超星网站开发实战答案,深圳报业集团官网,seo网站监测,seo整站优化方法目录 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.LazySingleton3764951d yxz.singleton.LazySingleton312b1dae可以看到这俩对象不一样。 但是使用枚举输出结果是一样的。 如果不让其破坏呢 我们在单例中添加一个方法。 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.LazySingleton3764951d yxz.singleton.LazySingleton3764951d这样输出结果是一样的了。因为反序列化创建对象时是通过反射创建的反射会调用我们自己的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.LazySingleton1b6d3586 yxz.singleton.LazySingleton4554617c坏了又是一样的这可怎么办 我们如何阻止呢 我们对代码的构造模式进行修改 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.LazySingleton1b6d3586 yxz.singleton.LazySingleton4554617c恭喜你仍然能出现两个但是我们先创建一个对象再使用反射呢 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.LazySingleton1b6d3586 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.w-s-a.com/news/682548/

相关文章:

  • 做教育行业营销类型的网站徐州做网站多少钱
  • 临沂品牌网站制作企业网站建设搜集资料
  • wordpress注册验证码手机网站优化
  • 往建设厅网站上传东西做衣服的教程网站有哪些
  • 网上商城网站设计免费咨询口腔科医生回答在线
  • 南京网站c建设云世家 s浏览器
  • 如何做镜像别人网站wordpress菜单对齐修改
  • 长春网站建设net企业公示信息查询官网
  • 金鹏建设集团网站可在哪些网站做链接
  • 电子产品网站开发背景网站关键词优化方案
  • 建网站论坛wordpress提交数据库错误
  • 国内网站建设公司开源网站系统
  • 网站开发公司上大连网站建设流程图
  • 银川网站seo宁波网
  • 个人备案网站会影响吗网站添加 备案
  • 网站建设与电子商务的教案关于旅游网站建设的方案
  • 电子商务网站建设设计原则找做网站找那个平台做
  • 天津高端品牌网站建设韶关网站建设墨子
  • Wordpress多站点为什么注册不了2008iis搭建网站
  • 天津高端网站制作建网站的公司服务
  • 温州网站推广优化类似淘宝的网站怎么做的
  • 网站建设实训考试什么网站做玩具的比较多
  • 上海网站建设特点怎样给公司做一个网站做推广
  • 流量网站怎么做的济南优化排名公司
  • 保定网站制作套餐设计师导航网站大全
  • 惠州 商城网站建设石家庄新闻广播在线收听
  • 洪山网站建设域名购买之后怎么做网站
  • 北京网站建设公司服务哪家好wap是什么意思?
  • 怎么看公司网站做的好不好哦wordpress页面目录下
  • 做装修业务呢有多少网站平台搭建是什么