Easy Tutorial
❮ Factory Pattern Mvc Pattern ❯

Transfer Object Pattern

The Transfer Object Pattern is used to transfer data with multiple attributes in one shot from client to server. The Transfer Object is also known as a Value Object. The Transfer Object is a simple POJO class with getter/setter methods and is serializable, so it can be transferred over the network. It has no behavior. The business class on the server side typically reads data from the database, populates the POJO, and sends it to the client or passes it by value. For the client, the Transfer Object is read-only. The client can create its own Transfer Object and pass it to the server to update values in the database at once. The following are the entities of this design pattern.

Implementation

We will create a StudentBO as the Business Object and StudentVO as the Transfer Object, both representing our entities.

The TransferObjectPatternDemo class acts as a client and will use StudentBO and Student to demonstrate the Transfer Object design pattern.

Step 1

Create the Transfer Object.

StudentVO.java

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

   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 the Business Object.

StudentBO.java

import java.util.ArrayList;
import java.util.List;

public class StudentBO {

   // List is treated as a database
   List<StudentVO> students;

   public StudentBO(){
      students = new ArrayList<StudentVO>();
      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");
   }

   // Retrieve student list from the 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

Demonstrate the Transfer Object design pattern using StudentBO.

TransferObjectPatternDemo.java

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

      // Print all students
      for (StudentVO student : studentBusinessObject.getAllStudents()) {
         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 student
      studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : "
      +student.getRollNo()+", Name : "+student.getName()+" ]");
   }
}

Step 4

Execute the program, output results:

Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]
❮ Factory Pattern Mvc Pattern ❯