超星网站开发实战答案,深圳报业集团官网,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这才没有创建多个。
当然我建议是使用枚举来组织它不过我在使用的时候还是没怎怎么故意使用反射或者序列化这些来破坏单例模式。