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. nginx 更新提示端口占用的解决办法

    最近更新ubuntu下的nginx,报了以下的错误, [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 可以看到,80 ...

  2. SQL Server窗口函数:ROWS与RANGE

    几乎每次我展示SQL Server里的窗口时,人们都非常有兴趣知道,当你定义你的窗口(指定的一组行)时,ROWS与RANGE选项之间的区别.因此在今天的文章里我想给你展示下这些选项的区别,对于你的分析 ...

  3. SQL语句技巧:查询时巧用OR实现逻辑判断

    首先看以下SQL逻辑语句块: ) ) SET @fieldname='chassisno' --这里可传入chassisno,plateno,owner,contacttelno其中之一或不传 SET ...

  4. Activating Browser Modes with Doctype

    原文地址:https://hsivonen.fi/doctype/ In order to deal both with content written according to Web standa ...

  5. FXForms,自动生成iOS表单

    1.简介 FXForms是一个简单的表单提交框架,他的作者是鼎鼎大名的 Nick Lockwood,你也许听说过他的其他的一些框架,比如 iCarousel. 为什么使用FxForms? 表单处理简单 ...

  6. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

  7. [CLR via C#]16. 数组

    数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...

  8. Asp.Net中动态页面转静态页面

    关于在Asp.Net中动态页面转静态页面的方法网上比较多.结合实际的需求,我在网上找了一些源代码,并作修改.现在把修改后的代码以及说明写一下. 一个是一个页面转换的类,该类通过静态函数Changfil ...

  9. sql server2008中怎样用sql语句创建数据库和数据表

    这是简单用代码实现创建数据库和数据表的sql语句,如下: --调用系统数据库-- use master go /***防止你要创建的数据库同名,先把它删除掉****/ if Exists(select ...

  10. 不经意间网易开源镜像去掉了FreeBSD的镜像

    http://mirrors.163.com/ FreeBSD已经到了这么不招人待见的地步了么? 网易曾经可是FreeBSD的大户啊.