CSC/ECE 517 Fall 2009/wiki2 18 ee

From Expertiza_Wiki
Jump to navigation Jump to search

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. [3]

Immutable objects aid in ensuring thread safety, since they cannot be changed. [4]

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. [6]

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. [6]

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. [1] We could visually represent it as follows:



To see this in code 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 Serializable and holds all the same information as the Person class.
  • An inner private method called writeReplace() is added to the Parent class. [2]

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.

[7]

Definitions

  • Serialization - The process of converting an object into a form more suitable for storage or transmission. [3]
  • Deserialization - The process of converting data read from disk or received from a transmission into object form. [3]
  • Immutable Object - An object who's attributes cannot be changed after creation. [5]
  • Thread Safety - the ability of a piece of software to be executed by multiple threads at the same time without malfunctioning. [4]

References