---
title: "Sington类的序列化留存"
author: "water84222"
date: "2011-08-01T14:30:32Z"
tags: []
categories: ["编程语言"]
slug: "sington"
draft: false
lastmod: "2011-08-01T14:30:32Z"
id: 1137271
url: "https://onlyLove.iteye.com/blog/1137271"
---

Sington类的序列化

为了使Singleton类变成可序列化的(serializable)，仅仅实现Serializable接口是不够的。为了维护 Singleton的单例性，你必须给Singleton类提供一个readResolve方法，否则的话，一个序列化的实例，每次反序列化的时候都会产 生一个新的实例。Singleton 也不会例外。

 

through the serializable tools, someone can write a **singleton** instance to disk, and then read it back up, effectively getting a new instance. Even though the constructor is private, the serializable tools have special access to create instances of a class regardless. Serialization has a special hook it uses - a private method on the class being instantiated called`readResolve()` - which is meant to supply a 'hook' for a class developer to ensure that they have a say in what object is returned by serialization. Oddly enough, `readResolve()` is not static, but is instead invoked on the new instance just created by the serialization. We'll get into that in a minute - for now, here is how our `readResolve()` method works with our **singleton**:

 

如下所示：

Java代码  [![收藏代码](http://www.iteye.com/images/icon_star.png)]()

01. import java.io.FileInputStream;
02. import java.io.FileOutputStream;
03. import java.io.ObjectInputStream;
04. import java.io.ObjectOutputStream;
05. import java.io.ObjectStreamException;
06. import java.io.Serializable;
07. //Singleton with final field
08. public class Singleton implements Serializable{
09. private static final long serialVersionUID = 5765648836796281035L;
10. public static final Singleton uniqueInstance = new Singleton();
11. private Singleton(){
12. }
13. //...Remainder omitted
14. public static void main(String\[] args) throws Exception{
15. //序列化
16. ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\\\Singleton.obj"));
17. Singleton singleton = Singleton.uniqueInstance;
18. objectOutputStream.writeObject(singleton);
19. objectOutputStream.close();
20. //反序列化
21. ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\\\Singleton.obj"));
22. Singleton singleton2 = (Singleton)objectInputStream.readObject();
23. objectInputStream.close();
24. //比较是否原来的实例
25. System.out.println(singleton==singleton2);
26. }
27. }

 输出结果为:false

解决方法是为Singleton类增加readResolve()方法：

Java代码  [![收藏代码](http://www.iteye.com/images/icon_star.png)]()

1. //readResolve 方法维持了Singleton的单例属性
2. private Object readResolve() throws ObjectStreamException{
3. return uniqueInstance;
4. }

 再进行测试：输出结果为true

 

反序列化之后新创建的对象会先调用此方法，该方法返回的对象引用被返回，取代了新创建的对象。本质上，该方法忽略了新建对象，仍然返回类初始化时创建的那个实例。