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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 30: Line 30:




==The Proxy Object==


  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; }
 
    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);
        }
    }
  }
http://stackoverflow.com/questions/702357/what-is-the-serialization-proxy-pattern
http://lingpipe-blog.com/2009/08/10/serializing-immutable-singletons-serialization-proxy/
http://lingpipe-blog.com/2009/08/10/serializing-immutable-singletons-serialization-proxy/



Revision as of 23:02, 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.


The Proxy Object

 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; } 
 
    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);
       }
   }
 }


http://stackoverflow.com/questions/702357/what-is-the-serialization-proxy-pattern http://lingpipe-blog.com/2009/08/10/serializing-immutable-singletons-serialization-proxy/

Definitions

- serialization
- deserialization
- imitable
- thread safety


References