Serialization proxy pattern

  1. private static nested class (has a single constructor whose parameter type is the enclosing class, the constructor just copies all the data of its argument) of the serializable class that concisely represents the logical state of an instance of the enclosing class.  

    // Serialization proxy for Period class

    private static class SerializationProxy implements Serializable {

    private final Date start;

    private final Date end;

    SerializationProxy(Period p) {

    this.start = p.start;

    this.end = p.end;

    }

    private static final long serialVersionUID = 234098243823485285L; // Any number will do (Item 75)

    }

  2. Both the enclosing class and its serialization proxy class must be declared to implement serializable.
  3. Add writeRepalce method in the enclosing class.

    // writeReplace method for the serialization proxy pattern

    private Object writeReplace() {

    return new SerializationProxy(this);

    }

  4. To guarantee no generation of a serialized instance of the enclosing class fabrication readObject method should be added.

// readObject method for the serialization proxy pattern

private void readObject(ObjectInputStream stream)

throws InvalidObjectException {

throw new InvalidObjectException("Proxy required");

}

  1. Finally, provide a readResolve method on the SerializationProxy class that returns a logically equivalent instance of the enclosing class.

    // readResolve method for Period.SerializationProxy

    private Object readResolve() {

    return new Period(start, end); // Uses public constructor

    }

Advantage of serialization proxy

The serialization proxy pattern allows the deserialized instance to have a different class from the originally serialized instance.

// EnumSet's serialization proxy

private static class SerializationProxy <E extends Enum<E>>

implements Serializable {

// The element type of this enum set.

private final Class<E> elementType;

// The elements contained in this enum set.

private final Enum[] elements;

SerializationProxy(EnumSet<E> set) {

elementType = set.elementType;

elements = set.toArray(EMPTY_ENUM_ARRAY); // (Item 43)

}

private Object readResolve() {

EnumSet<E> result = EnumSet.noneOf(elementType);

for (Enum e : elements)

result.add((E)e);

return result;

}

private static final long serialVersionUID = 362491234563181265L;

}

Limitations of serialization proxy

  1. It is not compatible with classes that are extendable by their clients (Item 17).
  2. It is not compatible with some classes whose object graphs contain circularities.
  3. Performance of it is worse than defensive copying.

Summary

Consider the serialization proxy pattern whenever you find yourself having to write a readObject or writeObject method on a class that is not extendable by its clients. This pattern is perhaps the easiest way to robustly serialize objects with nontrivial invariants.

Effective Java 78 Consider serialization proxies instead of serialized instances的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 11.序列化

    Chapter 11 Serialization Item 74: Implement Serializable judiciously 让一个类的实例可以被序列化不仅仅是在类的声明中加上" ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. effective java 读后感

    think in java  , effective java  这两本书一直都在java的生态圈中经久不衰.本来想着先翻过 think in java 这本大山,但是读到一半就放弃了.过长的篇幅,让 ...

  6. Effective Java阅读笔记——引言

    “我很希望10年前就拥有这本书.可能有人认为我不需要任何Java方面的书籍,但是我需要这本书.” ——Java之父 James Gosling 在图书馆找到这本java著作时,首先看到了这句话.   ...

  7. EFFECTIVE JAVA 第十一章 系列化

    EFFECTIVE  JAVA  第十一章  系列化(将一个对象编码成一个字节流) 74.谨慎地实现Serializable接口 *实现Serializable接口付出的代价就是大大降低了“改变这个类 ...

  8. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

  9. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

随机推荐

  1. SQL Server中的事务日志管理(5/9):完整恢复模式里的日志管理

    当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...

  2. [Solution] 简单数字识别之Tesseract

    图像识别涉及的理论:傅里叶变换,图形形态学,滤波,矩阵变换等等. Tesseract的出现为了解决在没有这些复杂的理论基础,快速识别图像的框架. 准备: 1.样本图像学习,预处理 (平均每1个元素出现 ...

  3. “康园圈--互联网+校园平台“项目之sprint2

    一.sprint2任务列表 1.部署框架,并上传代码到github. 2.原型设计 * 设计首页界面原型(包括功能公告.快速通道等展示栏) * 设计店铺浏览页面原型 * 设计店内浏览页面原型 * 设计 ...

  4. 组合数学 - 母函数的运用 + 模板 --- hdu : 2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 重构第20天 提取子类(Extact SubClass)

    理解:提取子类就是把基类中,不是所有子类或者只有少数子类用到的方法,提取出来,调整到子类中去. 详解:下面的代码中我们用到一个单一的类Registration,来处理学生选课信息. public cl ...

  6. 关于window.onload,window.onbeforeload与window.onunload

    ★  window.onload  当页面加载完毕的时候执行,即在当前页面进行其他操作之前执行.如,刚进入某个网页的弹窗提示. (  与window.onload相近的可以参考我写的另外一篇记录&qu ...

  7. XmlNodeList循环读取节点值

    foreach (XmlNode item in XmlNodeList) { string oid = item.SelectSingleNode("oid").InnerTex ...

  8. 重新想象 Windows 8 Store Apps (58) - 微软账号

    [源码下载] 重新想象 Windows 8 Store Apps (58) - 微软账号 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 微软账号 获取微软账号的用户 ...

  9. 在Android设备上判断设备是否支持摄像头

    private boolean hasCamera(){ boolean hasCamera=false; PackageManager pm=getActivity().getPackageMana ...

  10. 一个小笔记(7):EN_1

    For nearly ten years, the Unified Modeling Language(UML) has been the industry standard for visualiz ...