为了使一个单例类变成可串行化的,仅仅在声明中添加“implements Serializable”是不够的。因为一个串行化的对象在每次返串行化的时候,都会创建一个新的对象,而不仅仅是一个对原有对象的引用。为了防止这种情况,可以在单例类中加入readResolve 方法。
下面我们先简要地回顾下对象的序列化. 一般来说, 一个类实现了 Serializable接口, 我们就可以把它往内存地写再从内存里读出而"组装"成一个跟原来一模一样的对象. 不过当序列化遇到单例时,这里边就有了个问题: 从内存读出而组装的对象破坏了单例的规则. 单例是要求一个JVM中只有一个类对象的, 而现在通过反序列化,一个新的对象克隆了出来.
如下例所示:
- Java代码
-
- public final class MySingleton implements Serializable {
- private MySingleton() { }
- private static final MySingleton INSTANCE = new MySingleton();
- public static MySingleton getInstance() { return INSTANCE; }
- }
-
- 当把 MySingleton对象(通过getInstance方法获得的那个单例对象)序列化后再从内存中读出时, 就有一个全新但跟原来一样的MySingleton对象存在了. 那怎么来维护单例模式呢?这就要用到readResolve方法了.如下所示:
Java代码
- public final class MySingleton implements Serializable{
- private MySingleton() { }
- private static final MySingleton INSTANCE = new MySingleton();
- public static MySingleton getInstance() { return INSTANCE; }
- private Object readResolve() throws ObjectStreamException {
- // instead of the object we're on,
- // return the class variable INSTANCE
- return INSTANCE;
- }
- }
这样当JVM从内存中反序列化地"组装"一个新对象时,就会自动调用这个 readResolve方法来返回我们指定好的对象了, 单例规则也就得到了保证 。
- 下面的这个例子 更能很好的说明
- readResove的必要性
- So far so good. Things get a little complicated when dealing with more than one instance however. To explain this, I'll show this using a type-safe enumeration. Keep in mind that Java 5's enum type automatically handles this readResolve case for you. Here is a nice little enumeration:
- public final class Sides {
- private int value;
- private Sides(int newVal) { value = newVal; }
- private static final int LEFT_VALUE = 1;
- private static final int RIGHT_VALUE = 2;
- private static final int TOP_VALUE = 3;
- private static final int BOTTOM_VALUE = 4;
- public static final LEFT = new Sides(LEFT_VALUE);
- public static final RIGHT = new Sides(RIGHT_VALUE);
- public static final TOP = new Sides(TOP_VALUE);
- public static final BOTTOM = new Sides(BOTTOM_VALUE);
- }
- Now, implementing serialization, the key to determining which instance to return is in inspecting what value is set on the object itself:
- public final class Sides implements Serializable {
- private int value;
- private Sides(int newVal) { value = newVal; }
- private static final int LEFT_VALUE = 1;
- private static final int RIGHT_VALUE = 2;
- private static final int TOP_VALUE = 3;
- private static final int BOTTOM_VALUE = 4;
- public static final LEFT = new Sides(LEFT_VALUE);
- public static final RIGHT = new Sides(RIGHT_VALUE);
- public static final TOP = new Sides(TOP_VALUE);
- public static final BOTTOM = new Sides(BOTTOM_VALUE);
- private Object readResolve() throws ObjectStreamException {
- // Switch on this instance's value to figure out which class variable
- // this is meant to match
- switch(value) {
- case LEFT_VALUE: return LEFT;
- case RIGHT_VALUE: return RIGHT;
- case TOP_VALUE: return TOP;
- case BOTTOM_VALUE: return BOTTOM;
- }
- return null;
- }
- }
题外话,介绍一种更简单的创建单例对象的方法:
- public static final Singleton INSTANCE = new Singleton();
-