Transfer Object Pattern

The Transfer Object Pattern is used to pass data with multiple attributes from the client to the server in one go. Transfer objects are also known as value objects. A transfer object is a simple POJO class with getter/setter methods; it is serializable, so it can be transmitted over the network. It does not have any behavior. Business classes on the server side typically read data from the database, populate the POJO, and send it to the client or pass 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 in order to update the values in the database in one go. The following are the entities of this design pattern.

  • Business Object- Business service that populates data into transfer objects.
  • Transfer Object- Simple POJO, only has methods for setting/getting properties.
  • Client- Client can send requests or send transfer objects to business objects.

summary

intent

Used to simplify data transfer between network or application layers. It encapsulates data by creating a class with multiple properties, these properties represent the data to be transferred.

Main problem solved

  • Solves efficiency and encapsulation issues when data is transferred between layers in distributed systems, especially in multi-tier applications.

Use Case

  • When large amounts of data need to be transferred between client and server, especially in web services or remote procedure calls (RPC).

Implementation Approach

  • Transfer object class: Define a class containing multiple attributes, these attributes represent the data to be transferred.
  • Data access layer: Responsible for extracting data from database or other data sources and populating it into transfer objects.
  • Business logic layer: Optional, handles business logic, may need to use transfer objects.
  • Presentation layer or client: Uses transfer objects to display data or as a data carrier for user input.

Key code

  • Transfer object class: Contains attributes and possible business logic, for data encapsulation and transfer.

Application example

  • Web application: Presentation layer obtains or sends data from/to server through transfer objects.

Advantages

  1. Reduce network communication overhead: Reduces the number of network requests by transferring multiple data at once.
  2. Encapsulation: Transfer object serves as an encapsulation carrier for data, hiding internal data structure.
  3. Easy to maintain: Centrally manages transferred data, easy to modify and extend.

Disadvantages

  • Overuse: May increase system complexity due to creating too many transfer objects.

Usage suggestions

  • When large amounts of data need to be transferred between different layers in a multi-tier application, consider using the Transfer Object pattern.

Notes

  • Transfer objects should remain lightweight and avoid containing excessive business logic.

The several main roles involved

  1. Transfer Object Class:

    • Encapsulates the data that needs to be transferred.
  2. Data Access Layer:

    • Provides data and may be responsible for populating the data into transfer objects.
  3. Business Logic Layer (Optional):

    • Optional, handles business logic, may use transfer objects.
  4. Presentation Layer or Client:

    • Uses transfer objects to display data or send requests.

The Transfer Object pattern provides an effective way to reduce network communication times and simplify the data transfer process by encapsulating transferred data into an object.


Implementation

We will create one as the business objectStudentBOand one as the transfer objectStudentVO, which both represent our entity.

TransferObjectPatternDemoThe class here acts as a client and will useStudentBOandStudentto demonstrate the Transfer Object design pattern.

传输对象模式的 UML 图

Step 1

Create a 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 a business object.

StudentBO.java

import java.util.ArrayList; import java.util.List; public class StudentBO { //Lists are 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 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

UsageStudentBOto demonstrate the Transfer Object design pattern.

TransferObjectPatternDemo.java

public class TransferObjectPatternDemo { public static void main(String[] args) { StudentBO studentBusinessObject = new StudentBO(); //Output 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

Run the program and output the result:

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