序列化破坏单例模式原理解析及解决方案

  将单例对象序列化和反序列化操作后得到的新对象与原单例对象并不相同,因为反序列化是通过反射获取的新对象。

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        HungrySingleton hungrySingleton = HungrySingleton.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        oos.writeObject(hungrySingleton);

        File file = new File("singleton_file");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        HungrySingleton newHungrySingleton = (HungrySingleton) ois.readObject();

        System.out.println(hungrySingleton);
        System.out.println(newHungrySingleton);
        System.out.println(hungrySingleton==newHungrySingleton);
    }
}

输出结果:
  designmodel.singleton.HungrySingleton@1d44bcfa
  designmodel.singleton.HungrySingleton@b4c966a
  false

  但是可以通过在单例类中添加readResolve方法解决该问题,但是该单例类必须实现SerializableExternalizable接口。

public class HungrySingleton implements Serializable {
    private static final HungrySingleton hungrySingleton;
    static {
        hungrySingleton = new HungrySingleton();
    }

    public static HungrySingleton getInstance(){
        return hungrySingleton;
    }

    private Object readResolve(){
        return hungrySingleton;
    }
}  

  readResolve方法并不是重写Object类中的方法,具体实现的原理可从源码中得到解释。

/**
     * Returns true if represented class is serializable or externalizable and
     * defines a conformant readResolve method.  Otherwise, returns false.
     */
    boolean hasReadResolveMethod() {
        requireInitialized();
        return (readResolveMethod != null);
    }  

输出结果:
  designmodel.singleton.HungrySingleton@1d44bcfa
  designmodel.singleton.HungrySingleton@1d44bcfa
  true

发布了11 篇原创文章 · 获赞 1 · 访问量 262

猜你喜欢

转载自blog.csdn.net/Introncheng/article/details/103132123