Transfer object mode
Transfer Object Pattern is used to transfer data with multiple attributes from the client to the server at one time. Transfer objects are also called numerical objects. The transfer object is a simple POJO class with getter/setter methods, and it is serializable so it can be transferred over the network. It has no behavior. Server-side business classes typically read data from the database, populate the POJO, and send it to the client or pass it by value. To the client, transport objects are read-only. The client can create its own transfer object and pass it to the server to update the values in the database in one go. The following are the entities of this design pattern.
Business Object (Business Object) - Business service that populates data for transfer objects.
Transfer Object - Simple POJO, only methods to set/get properties.
Client - The client can send requests or send transport objects to business objects.
Implementation
We will create a StudentBO as a business object and a StudentVO as a transfer object, both of which represent our entity.
TransferObjectPatternDemo, our demo class is here as a client and will use StudentBO and Student to demonstrate the transfer object design pattern.
Step 1
Create a transport 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<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"); } //从数据库中检索学生名单 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 StudentBO to demonstrate the transfer object design pattern.
TransferObjectPatternDemo.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
Verify the output.
Student: [RollNo : 0, Name : Robert ] Student: [RollNo : 1, Name : John ] Student: Roll No 0, updated in the database Student: [RollNo : 0, Name : Michael ]