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 numeric objects. The transport object is a simple POJO class with a getter/setter method, which is serializable, so it can be transmitted over the network. It doesn’t do anything. Business classes on the server side usually 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 transport object and pass it to the server to update the values in the database at once. The following are the entities of this design pattern.
业务对象(Business Object) -A business service that populates the transfer object with data.
传输对象(Transfer Object) -simple POJO with only methods to set / get properties.
客户端(Client) -the client can send a request or send a transfer object to a business object.
6.35.1. Realize ¶
As a business object, we will create a StudentBO And as a transfer object StudentVO They all represent our entities.
TransferObjectPatternDemo Class is here as a client and will use the StudentBO And Student To demonstrate the transport object design pattern.
6.35.2. Step 1 ¶
Create a transfer object.StudentVO.java ¶
publicclassStudentVO{privateStringname;privateintrollNo;StudentVO(Stringname,introllNo){this.name=name;this.rollNo=rollNo;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetRollNo(){returnrollNo;}publicvoidsetRollNo(introllNo){this.rollNo=rollNo;}}
6.35.3. Step 2 ¶
Create a business object.StudentBO.java ¶
importjava.util.ArrayList;importjava.util.List;publicclassStudentBO{//列表是当作一个数据库List<StudentVO>students;publicStudentBO(){students=newArrayList<StudentVO>();StudentVOstudent1=newStudentVO("Robert",0);StudentVOstudent2=newStudentVO("John",1);students.add(student1);students.add(student2);}publicvoiddeleteStudent(StudentVOstudent){students.remove(student.getRollNo());System.out.println("Student:
Roll No"+student.getRollNo()+", deleted from
database");}//从数据库中检索学生名单publicList<StudentVO>getAllStudents(){returnstudents;}publicStudentVOgetStudent(introllNo){returnstudents.get(rollNo);}publicvoidupdateStudent(StudentVOstudent){students.get(student.getRollNo()).setName(student.getName());System.out.println("Student:
Roll No"+student.getRollNo()+", updated in the database");}}
6.35.4. Step 3 ¶
Use StudentBO To demonstrate the transport object design pattern.TransferObjectPatternDemo.java ¶
publicclassTransferObjectPatternDemo{publicstaticvoidmain(String[]args){StudentBOstudentBusinessObject=newStudentBO();//输出所有的学生for(StudentVOstudent:studentBusinessObject.getAllStudents()){System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name
:"+student.getName()+"]");}//更新学生StudentVOstudent=studentBusinessObject.getAllStudents().get(0);student.setName("Michael");studentBusinessObject.updateStudent(student);//获取学生studentBusinessObject.getStudent(0);System.out.println("Student:
[RollNo :"+student.getRollNo()+", Name :"+student.getName()+"]");}}
6.35.5. Step 4 ¶
Execute 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 ]