Serialization

Rule 6:Consider serialization proxies instead of serialized instances

pre-ques?
serialization proxies ?

serialization proxies vs serialized instance.

serialization proxy pattern reduces the probable bugs of serialization.
Following are simple steps to create Serialization proxy pattern:
1.First, design a
private static nested class that concisely represents the logical state of an instance
of the enclosing class. This nested class is known as the serialization proxy of the
enclosing class.
2.It should have a single constructor, whose parameter type is the
enclosing class. This constructor merely copies the data from its argument.
3.Both the enclosing class and its serialization proxy must be
declared to implement Serializable.
4.add writeReplace method to the enclosing class. the writeReplace method translates an instance of the
enclosing class to its serialization proxy prior to serialization

In 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.

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment