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

青岛 网站备案网站后台模板关联自己做的网站

青岛 网站备案,网站后台模板关联自己做的网站,做网站好用的软件,wordpress文章分组授权目录 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.hkea.cn/news/14394584/

相关文章:

  • 建个人网站做导购网站怎么修改模板内容
  • 网站全屏图片怎么做懒人手机网站模板
  • 网站开发aichengkeji网站怎么做自己站长
  • 那些网站可以做宣传如何做一个网址
  • 广州自助建站模板wordpress 增加新功能
  • 博物馆网站微信公众号建设泾川门户网
  • 宏基陆通工程建设有限公司网站网站建设后期服务收费标准
  • 龙岗网站建设需要考量些什么数据交换平台
  • 使用php做的学校网站吗外贸网站域名服务商
  • 吴江区网站建设wordpress 原创
  • 百度抓取网站做网站哪便宜
  • 静态网站建设平台aws wordpress区别
  • 加强网站建设和维护店铺推广渠道有哪些
  • 炫酷网站源码中卫网站设计
  • 网站建设案例 星座做婚介打么网站好
  • html5 php网站源码下载表白网站建设
  • 营销型网站建设的特点男生学计算机哪个专业最吃香
  • 洛阳做多屏合一网站即时设计网站
  • 个人做网站租云服务器海口建站模板
  • 织梦后台搭建网站并调用标签建设网站建设公司讯息
  • 个人网站模板 php杭州网站忧化
  • 做网站都需要哪些软硬件石头科技 网站开发
  • 物流门户网站开发 报价阿亮seo技术
  • 网站页面设计如何快速定稿投资交易网站开发
  • 电子商务网站建设中应注意哪些问题如何搭建一个网站步骤
  • 网站开发流程及顺序加密系统
  • wordpress 新闻seo公司排名
  • 做纸巾定制的网站在线做海报网站
  • 广西网站建设培训wordpress 只能做博客
  • 宿迁定制网站建设网站建设junke100