6.35. Transfer object mode

发布时间 : 2025-10-25 13:34:18 UTC      

Page Views: 10 views

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.

传输对象模式的 UML 图

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");}//从数据库中检索学生名单publicListgetAllStudents(){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 ] 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。