Friday, November 03, 2006

Transfer Object Design Pattern

Benefits of Transfer Object Pattern
1. Fetch multiple values in one trip.
2.Decrease network traffic.
3.Get related values from remote business method.
4. Minimize latency and server resource usage.

What is Transfer Object Pattern?
A transfer object is a serializable class that groups related attributes, forming a composite value. This class is used as the return type of a remote business method. Clients receive instances of this class by calling coarse-grained business methods, and then locally access the fine-grained values within the transfer object. Fetching multiple values in one server roundtrip decreases network traffic and minimizes latency and server resource usage.

Example of Transfer Object
The EmployeeDetails and EmployeeTO transfer objects.
Sample application class EmployeeAdminFacade has a method getEmployeeInfo that returns an immutable, serializable collection of EmployeeDetails objects, each of which is a transfer object that represents data from an employee. The collection returned by method getEmployeeInfo is also a transfer object, because the values it contains are always accessed together where they are used. This hierarchical transfer object is implemented by class EmployeeTO.




Above Figure shows the structure of the EmployeeTO transfer object. Class mutableEmployeeTO, which extends a serializable ArrayList, contains a collection of EmployeeDetails objects. But because MutableEmployeeTO extends ArrayList, it is mutable. Transfer objects should be immutable so that clients do not unintentionally change their contents. Interface EmployeeTO adapts the MutableEmployeeTO collection, allowing read-only access to the collection while preventing modifications.

The definition of the EmployeeTO interface follows.

public interface EmployeeTO extends Serializable {

public Iterator iterator();

public int size();

public boolean contains(Object o);

public boolean containsAll(Collection c);

public boolean equals(Object o);

public int hashCode();

public boolean isEmpty();

public Object[] toArray();

public Object[] toArray(Object[] a);

static class MutableEmployeeTO extends ArrayList implements EmployeeTO { }

} //end of the interface

The EmployeeTO interface is clearly a collection, since it defines the Java collection methods. The collection is immutable, because it has no methods that would allow the collection contents to be changed. An example of using EmployeeTO appears in method EmployeeAdminFacadeEJB. getEmployeeInfo, shown below. The method constructs a EmployeeTO.MutableEmployeeTO object and populates it with EmployeeDetails instances. Note that the return type of getEmployeeInfo is EmployeeTO, so callers can access only methods that read the collection contents, and not methods that would change them.

public EmployeeTO getEmployeeInfo(int employeeId) throws Exception {

EmployeeTO.MutableEmployeeTO retVal = new EmployeeTO.MutableEmployeeTO();

EmployeeManagerLocal mgr = getEmpMgr();

try {

EmployeeLocalHome pohome = getEmpInfo();

Collection employee = mgr.getEmpById(employeeId);

Iterator it = employee.iterator();

while((it!= null) && (it.hasNext())) {

....... // Access and format data

retVal.add(new EmployeeDetails(employeeId, joiningDate,salary,department));

}

} catch (FinderException fe) {

... // process exception

}

return(retVal);

} //end of method

The details for a single employee are contained in an immutable, serializable transfer object of type employeeDetails It is a typical transfer object for a single composite value, containing private fields and public, read-only property accessors, as shown in the following code sample.

public class EmployeeDetails implements java.io.Serializable {

private int employeeId;

private Date joiningDate;

private float salary;

private String department;

public EmployeeDetails(int employeeId, Date joiningDate, float salary, String department) {

this.employeeId = employeeId;

this.joiningDate = joiningDate;

this.salary = salary;

this.department = department;

}

public int getEmployeeId() { return employeeId; }

public Date getJoiningDate () { return joiningDate; }

public float getSalary () { return salary; }

public String getDepartment () { return department; }

}

1 comment:

Anonymous said...

It would have be better if you would have added the source for example given in this Pattern.