CSC/ECE 517 Fall 2009/wiki2 18 ee: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 13: Line 13:
One issue with serialization under Java is the need for serialization of immutable classes.  Take the following class for example:
One issue with serialization under Java is the need for serialization of immutable classes.  Take the following class for example:


   public class person {
   public class Person {
     String name;
     String name;
     Integer age;
     Integer age;
      
      
     public void person(String name, Integer age) {
     public Person(String name, Integer age) {
         this.name = name;
         this.name = name;
         this.age = age;
         this.age = age;
Line 26: Line 26:




The class is easily serializable with no changes, and could be written to disk easily.  A problem arises when the object needs to be deserialized.
The class is easily serializable with no changes, and could be written to disk easily.  A problem arises when the object needs to be deserialized. Normally when Java is deserializing an object, it would call the default constructor Person() and then call setFoo() on each object attribute.  With this object there is no default constructor, nor are there setters for the object's attributes.  This is where the Serialization Proxy pattern comes into play. 




Line 35: Line 35:
==Definitions==
==Definitions==
  - serialization
  - serialization
- deserialization
  - imitable
  - imitable
  -  
  - thread safety




==References==
==References==

Revision as of 04:39, 8 October 2009

Serialization Proxy Design Pattern

During system design, immutable classes are preferred to guarantee thread safety and consistent object state. For instance, serialization of immutable classes could cause issues in Java if immutability must be preserved. The Serialization Proxy Pattern aims to make serialization acceptable in that regard. Discuss serialization proxy pattern with regards to the problem, design, implementation details, and idiomatic usage.


Introduction

When the need to serialize objects in Java arises, the most common tool is the Serializable interface. This interface is simply used to indicate that the object is serializable. The class may implement the writeReplace() method. If it does, during serialization the object returned from this method will be used for serialization instead of the object itself.

The Problem

One issue with serialization under Java is the need for serialization of immutable classes. Take the following class for example:

 public class Person {
    String name;
    Integer age;
    
    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public String name() { return name; }
    public Integer age() { return age; } 
 }


The class is easily serializable with no changes, and could be written to disk easily. A problem arises when the object needs to be deserialized. Normally when Java is deserializing an object, it would call the default constructor Person() and then call setFoo() on each object attribute. With this object there is no default constructor, nor are there setters for the object's attributes. This is where the Serialization Proxy pattern comes into play.



http://lingpipe-blog.com/2009/08/10/serializing-immutable-singletons-serialization-proxy/

Definitions

- serialization
- deserialization
- imitable
- thread safety


References