Transfer Object Pattern

Transfer Object Pattern is used when developer wants to pass data with multiple attributes in one shot from client to server. Transfer Object is also known as Value Object. Transfer Object is a POJO class having getter/setter methods and is serializable so that it can be transferred over the network. It does not have any behavior. Server Side business class normally fetches data from the database and fills the POJO and sends it to the client or passes it by value. For client, transfer object is read-only. Client can create its own transfer object and pass it to server to update values in database in one shot. Following are the entities of this type of design pattern :

  • Business Object – Business Service fills the Transfer Object with data
  • Transfer Object – Simple POJO having methods to set/get attributes only
  • Client – Client either requests or sends the Transfer Object to Business Object

In the following example there is a StudentBO as Business Object, StudentVO as Transfer Object (Value Object) representing entities. TransferObjectPatternDemo is acting as a client and will use StudentBO and StudentVO to demonstrate Transfer Object Design Pattern.

Transfer Object Pattern UML Diagram

Step 1 : Create Transfer Object

public class StudentVO {
private String name;
private int rollNo;

public StudentVO(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}

Step 2 : Create Business Object

public class StudentBO {
// List is working as a database
List<StudentVO> students;

public StudentBO() {
students = new ArrayList<>();
StudentVO student1 = new StudentVO("Robert", 0);
StudentVO student2 = new StudentVO("John", 1);
students.add(student1);
students.add(student2);
}

public void deleteStudent(StudentVO student) {
students.remove(student.getRollNo());
System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database");
}

public List<StudentVO> getAllStudents() {
return students;
}

public StudentVO getStudent(int rollNo) {
return students.get(rollNo);
}

public void updateStudent(StudentVO student) {
students.get(student.getRollNo()).setName(student.getName());
System.out.println("Student: Roll No " + student.getRollNo() +", updated in the database");
}
}

Step 3 : Use Transfer Object to demonstrate Transfer Object Design Pattern

public class TransferObjectPatternDemo {
public static void main(String[] args) {
StudentBO studentBusinessObject = new StudentBO();

List<StudentVO> allStudents =studentBusinessObject.getAllStudents();
//print all students
for (StudentVO student : allStudents) {
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
}

//update student
StudentVO student = studentBusinessObject.getAllStudents().get(0);
student.setName("Michael");
studentBusinessObject.updateStudent(student);

//get the student
student = studentBusinessObject.getStudent(0);
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");

}
}

The output will be :

Student: [RollNo : 0, Name : Robert ]Student: [RollNo : 1, Name : John ]Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]

Leave a Reply

Your email address will not be published. Required fields are marked *