目录

传输对象模式(Transfer Object Pattern)

当我们想要从客户端到服务器一次性传递具有多个属性的数据时,使用传输对象模式。 传输对象也称为值对象。 Transfer Object是一个简单的POJO类,具有getter/setter方法,并且是可序列化的,因此可以通过网络传输。 它没有任何行为。 服务器端业务类通常从数据库中提取数据并填充POJO并将其发送到客户端或按值传递。 对于客户端,传输对象是只读的。 客户端可以创建自己的传输对象并将其传递给服务器,以便一次性更新数据库中的值。 以下是此类设计模式的实体。

  • Business Object - 业务服务使用数据填充传输对象。

  • Transfer Object - 简单的POJO,具有仅设置/获取属性的方法。

  • Client - 客户端请求或将传输对象发送到业务对象。

实现 (Implementation)

我们将创建一个StudentBO作为业务对象, Student作为转移对象来表示我们的实体。

TransferObjectPatternDemo ,我们的演示类,在这里充当客户端,将使用StudentBOStudent来演示传输对象设计模式。

传递对象模式UML图

Step 1

创建传输对象。

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

创建业务对象。

StudentBO.java

import java.util.ArrayList;
import java.util.List;
public class StudentBO {
   //list is working as a database
   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");
   }
   //retrive 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

使用StudentBO演示传输对象设计模式。

TransferObjectPatternDemo.java

public class TransferObjectPatternDemo {
   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);
      student.setName("Michael");
      studentBusinessObject.updateStudent(student);
      //get the student
      student = studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
   }
}

Step 4

验证输出。

Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]
↑回到顶部↑
WIKI教程 @2018