Singleton模式指出,一个类可以有一个实例,并且不允许创建多个实例。为此,我们将类的构造函数设为私有,并通过静态方法返回实例。但是使用序列化,我们仍然可以创建一个类的多个实例。请参阅下面的示例-
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{ public static void main(String[] args) throws ClassNotFoundException, IOException{ A a = A.getInstance(); A b = (A) getSerializedCopy(a); System.out.println(a.hashCode()); System.out.println(b.hashCode()); } public static Object getSerializedCopy(Object sourceObject) throws IOException, ClassNotFoundException { ObjectOutputStream objectOutputStream = null; ObjectInputStream objectInputStream = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(sourceObject); objectOutputStream.flush(); objectInputStream = new ObjectInputStream( new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); return objectInputStream.readObject(); } } class A implements Serializable { private static A a; private A(){} public static A getInstance(){ if(a == null){ a = new A(); } return a; } }
输出结果
1550089733 865113938
在这里您可以看到,我们已经创建了Singleton类的另一个对象。让我们看看如何防止这种情况-
readResolve()
单例类中的Override方法。
// implement readResolve method protected Object readResolve() { return a; }
输出结果
1550089733 1550089733