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. 基于openssl的单向和双向认证

    1.前言 最近工作涉及到https,需要修改nginx的openssl模块,引入keyless方案.关于keyless可以参考CloudFlare的官方博客: https://blog.cloudfl ...

  2. 3D拓扑自动布局之Node.js篇

    上篇将3D弹力布局的算法运行在Web Workers后台,这篇我们将进一步折腾,将算法运行到真正的后台:Node.js,事先申明Node.js篇和Web Workers篇一样,在这个应用场景下并不能提 ...

  3. SpringMVC——对Ajax的处理(包含 JSON 类型)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

  4. 重构第12天 分解依赖(Break Dependencies)

    理解:“分解依赖” 是指对部分不满足我们要求的类和方法进行依赖分解,通过装饰器来达到我们需要的功能. 详解:今天我们所讲的这个重构方法对于单元测试是非常有用的.如果你要在你的代码中加入单元测试但有一部 ...

  5. Unity多语言本地化改进版

    简介 之前捣鼓过一个通过csv配置游戏多语言支持的小工具,但是发现使用过程中,通过notepad++去进行转码很不方便,并且直接将配置的csv不加密的放在游戏中心里感觉不是很踏实 于是乎~~ 新的方案 ...

  6. Winform里面的缓存使用

    缓存在很多情况下需要用到,合理利用缓存可以一方面可以提高程序的响应速度,同时可以减少对特定资源访问的压力.本文主要针对自己在Winform方面的缓存使用做一个引导性的介绍,希望大家能够从中了解一些缓存 ...

  7. HTML5 Wijmo:控制 Wijmo Grid 插件的编辑模式

    Wijmo jQuery 插件经常应用于在财务类网站中创建平滑和良好用户体验的交互表格.WijGrid 插件用于显示.排序.分组和编辑数据.今天我们来分享下如何控件WijGrid插件的编辑模式. 在本 ...

  8. UVA 10090 - Marbles 拓展欧几里得

    I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...

  9. CSS 最核心的四个概念(摘录)

    本文将讲述 CSS 中最核心的几个概念,包括:盒模型.position.float等.这些是 CSS 的基础,也是最常用的几个属性,它们之间看似独立却又相辅相成.为了掌握它们,有必要写出来探讨一下,如 ...

  10. Ahjesus Nodejs01 环境搭建及运行

    访问http://nodejs.org/,根据系统选择下载文件,我用的win7 64 安装一路下一步直到完成 运行cmd输入node -v查看是否安装成功 成功会显示版本号 到此环境搭建完毕 ==== ...