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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 11: Line 11:
Here is a dipiction of normal serialization.  This shows the Person class being serialized and de-serialized:
Here is a dipiction of normal serialization.  This shows the Person class being serialized and de-serialized:


[[File:Serial noproxy ee.jpg|caption]]
[[Image:Serial noproxy ee.jpg]]





Revision as of 03:32, 14 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

Serialization is the process of converting an object or many objects into a form that can be either written to disk or transmitted across the network. The idea being that these can be read back and loaded as objects within an environment, called de-serialization. Imagine having an application with user objects. When the application is shut down, the objects are written to disk. When the application is started back, the user objects are read from disk and initialized as objects. The objects themselves would be in the exact same state as they were before the system was shut down.

Here is a dipiction of normal serialization. This shows the Person class being serialized and de-serialized:


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.


Here is a simple example of serialization in java:

       ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("/tmp/foo"));
       stream.writeObject(myObject);
       stream.close();

This would serialize the object "myObject" into the file "/tmp/foo". Here is a simple example of deserialization:

       ObjectInputStream stream = new ObjectInputStream(new FileInputStream("/tmp/foo"));
       Object myObject = stream.readObject();
       stream.close();

This would take the data in /tmp/foo and deserialize it into the myObject object.

The Problem

One issue with serialization under Java is the need for serialization of immutable classes. These are classes that after creation, cannot be modified. 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; } 
 }

Once the constructor is called, neither name or age can be changed. This class is immutable.

The class is easily serializable with no changes, and could be written to disk easily. A problem arises when the object needs to be de-serialized.

Normally when Java is de-serializing an object, it would call the default constructor Person() and then call the setter on each object attribute. With this object there is no default constructor, nor are there setters for the object's attributes. This was done intentionally to keep the object from changing. Serialization Proxy pattern comes into play here.

The Proxy Object

To work around the fact that we can't deserialize an immutable class, we use a proxy object that is an inner class of the class we want to serialize. We would change the Person class as follows:


 public class Person {
    private String name;
    private Integer age;
    
    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public String name() { return name; }
    public Integer age() { return age; } 
 
    private Object writeReplace() {
       return new Proxy(this);
   }
   
 
    private static class Proxy implements Serializable {
       String name;
       Integer age;
 
       public Proxy() { }
       public Proxy(Person person) {
           name = person.name();  
           age = person.age();
       }
 
       Object readResolve() {
           return new Person(name, age);
       }
   }
 }


So what changed? A private static class was added that is Seriazliable and holds all the same information as the Person class. Also an inner private method called writeReplace() is added to the Parent class.

Serialization

During serialization, the following occurs:

  1. The object is checked to see if writeReplace() is defined for the Person object.
  2. The writeReplace() function is called to retrieve the proxy object.
  3. Serialization takes place on the proxy object instead.

Deserialization

During deserialization, the following occurs:

  1. The stored Proxy object is deserialized, resulting in a Proxy object. Since the member variables of the Proxy object are public, it can be deserialized with no issue.
  2. The Proxy object is examined to determine if it has a readResolve() method.
  3. Since it does, readResolve() is called, returning a new Person immutable object.

Benefits

  • The ability to serialize immutable objects.
  • The interface of the immutable object does not have to be changed

Drawbacks

  • Slower than normal serialization
  • Cannot handle some objects with circular object graphs. The following is an example of a circular object graph:
public class A {
   private B classB;
   public A(){}
   public getClassB() { return classB };
}
public class B {
   private A classA;
  
   public B(){}
   public getClassA() { return classA };
}

In this example, class A holds an instance to class B and class B holds an instance to class A.

Definitions

- serialization - The process of converting an object into a form more suitable for storage or transmission.  
- deserialization - The process of converting data read from disk or received from a transmission into object form.  
- immutable object - An object who's attributes cannot be changed after creation.  
- thread safety - the ability of a piece of software to be executed by multiple threads at the same time without malfunctioning.

References

http://stackoverflow.com/questions/702357/what-is-the-serialization-proxy-pattern
http://lingpipe-blog.com/2009/08/10/serializing-immutable-singletons-serialization-proxy/
http://en.wikipedia.org/wiki/Serialization
http://en.wikipedia.org/wiki/Thread_safety