zoukankan      html  css  js  c++  java
  • Effective Java 78 Consider serialization proxies instead of serialized instances

    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.

  • 相关阅读:
    (转)一文讲清TCP/IP 协议
    Java框架之Spring Boot和Spring Cloud区别
    php大力力 [006节]初步接触认识phpMyAdmin
    php大力力 [005节] php大力力简单计算器001
    php大力力 [004节]PHP常量MAMP环境下加载网页
    php大力力 [003节]php在百度文库的几个基础教程mac环境下文本编辑工具
    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动
    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
    C# DataSet与DataTable的区别和用法 ---转载
    【SqlServer】利用sql语句附加,分离数据库-----转载
  • 原文地址:https://www.cnblogs.com/haokaibo/p/consider-serialization-proxies-instead-of-serialized-instances.html
Copyright © 2011-2022 走看看