Transfer Object Pattern
# Transfer Object Pattern
## Transfer Object Pattern
The Transfer Object Pattern is used to transfer data with multiple attributes from the client to the server in a single call. A Transfer Object is also known as a Value Object. A Transfer Object is a simple POJO class with getter/setter methods. It is serializable so it can be transferred over the network. It has no behavior. The server-side business class 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 in one go. The following are the entities of this design pattern.
* **Business Object** - The business service that populates the Transfer Object with data.
* **Transfer Object** - A simple POJO with only setter/getter methods.
* **Client** - The client can send a request or send a Transfer Object to the business object.
* * *
## Overview
### Intent
To simplify data transfer between network or application layers. It encapsulates data by creating a class that contains multiple attributes representing the data to be transferred.
### Problem It Solves
* Addresses the efficiency and encapsulation issues of data transfer between layers in distributed systems, especially in multi-tier applications.
### When to Use
* When a large amount of data needs to be transferred between the client and the server, particularly in web services or Remote Procedure Calls (RPC).
### Implementation
* **Transfer Object Class**: Defines a class containing multiple attributes that represent the data to be transferred.
* **Data Access Layer**: Responsible for extracting data from the database or other data sources and populating the Transfer Object.
* **Business Logic Layer**: Optional, handles business logic and may use the Transfer Object.
* **Presentation Layer or Client**: Uses the Transfer Object to display data or as a data carrier for user input.
### Key Code
* **Transfer Object Class**: Contains attributes and possibly business logic for data encapsulation and transfer.
### Application Example
* **Web Application**: The presentation layer retrieves or sends data from/to the server via a Transfer Object.
### Advantages
1. **Reduces Network Communication Overhead**: Transfers multiple data items in one go, reducing the number of network requests.
2. **Encapsulation**: The Transfer Object acts as a carrier for data, hiding the internal data structure.
3. **Easy to Maintain**: Centralizes the management of transferred data, making it easier to modify and extend.
### Disadvantages
* **Overuse**: Creating too many Transfer Objects might increase system complexity.
### Usage Suggestion
* Consider using the Transfer Object Pattern when a large amount of data needs to be transferred between different layers in a multi-tier application.
### Notes
* Transfer Objects should remain lightweight and avoid containing excessive business logic.
### Main Roles Involved
1. **Transfer Object Class**:
* Encapsulates the data to be transferred.
2. **Data Access Layer**:
* Provides data and may be responsible for populating the Transfer Object.
3. **Business Logic Layer (Optional)**:
* Optional, handles business logic and may use the Transfer Object.
4. **Presentation Layer or Client**:
* Uses the Transfer Object to display data or send requests.
The Transfer Object Pattern provides an effective way to reduce the number of network communications and simplify the data transfer process by encapsulating the transferred data into an object.
* * *
## Implementation
We will create a _StudentBO_ as the business object and a _StudentVO_ as the transfer object, both representing our entity.
The _TransferObjectPatternDemo_ class here 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
```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
```java
import java.util.ArrayList;
import java.util.List;
public class StudentBO {
List 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 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 _StudentBO_ to demonstrate the Transfer Object design pattern.
## TransferObjectPatternDemo.java
```java
public class TransferObjectPatternDemo {
public static void main(String[] args) {
StudentBO studentBusinessObject = new StudentBO();
for (StudentVO student : studentBusinessObject.getAllStudents()) {
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
}
StudentVO student = studentBusinessObject.getAllStudents().get(0);
student.setName("Michael");
studentBusinessObject.updateStudent(student);
studentBusinessObject.getStudent(0);
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
}
}
## Step 4
Execute the program, and the output is:
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]
YouTip