It is used when we want to pass data with multiple attributes in one shot from client to server. Transfer Object is a simple POJO class having getter/setter methods and is serialized so that it can be transferred over the network. 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 clients, 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 shot.
Following are the entities of this type of design pattern:
Transfer Object | Simple POJO having methods to set/get attributes only |
---|---|
Business Object | Fills the Transfer Object with data |
Client | Either requests or sends the Transfer Object to Business Object |
Approach:
- Step 1: Create a Transfer Object
- Step 2: Create a Business Object.
- Step 3: Use the StudentBO to demonstrate Transfer Object Design Pattern
- Step 4: Verify the output.
Procedure:
Step 1: Creating a Transfer Object
Example
// Transfer Object Pattern - Design Pattern // Step 1 // Creating a Transfer Object // randomly be named it 'StudentVO.java' // Class StudentVO public class StudentVO {
// Member variables of class
private String name;
private int rollNo;
// Creating a constructor of above class
StudentVO(String name, int rollNo) {
// This keyword for assignment
// to same memory block created
// for every name and roll number of student
this .name = name;
this .rollNo = rollNo;
}
// Getting name of student
public String getName() {
return name;
}
// Setting name of Student
public void setName(String name) {
this .name = name;
}
// Getting roll number of student
public int getRollNo() {
return rollNo;
}
// Setting roll number of student
public void setRollNo( int rollNo) {
this .rollNo = rollNo;
}
} |
Step 2: Creating a Business Object
Example
// Transfer Object Pattern - Design Pattern // Step 2 // Creating a Business object // randomly be named it 'StudentBO.java' // Importing List and ArrayList classes of // java.util package import java.util.ArrayList;
import java.util.List;
// Class StudentBO public class StudentBO {
// List is working as a database
List<StudentVO> students;
public StudentBO() {
students = new ArrayList<StudentVO>();
// Adding custom inputs
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 list of students 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: Use the StudentBO to demonstrate Transfer Object Design Pattern
Implementation: List is acting as DB here as shown in demonstrating Transfer Object Design Pattern.
Example
// Transfer Object Pattern - Design Pattern // Step 3 // Use the StudentBO to demonstrate Transfer Object Design Pattern // randomly be named it 'TransferObjectPatternDemo.java' public class TransferObjectPatternDemo {
// Main driver method
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 );
// Custom input
student.setName( "Michael" );
studentBusinessObject.updateStudent(student);
// Getting the student
student = studentBusinessObject.getStudent( 0 );
System.out.println( "Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]" );
}
} |
Step 4: Verifying output
Student : [RollNo : 0, Name : Robert ] Student : [RollNo : 1, Name : John ] Student : Roll No 0, updated in the database Student : [RollNo : 0, Name : Michael ]