国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

設(shè)計模式 / 傳輸對象模式

傳輸對象模式

傳輸對象模式(Transfer Object Pattern)用于從客戶端向服務(wù)器一次性傳遞帶有多個屬性的數(shù)據(jù)。傳輸對象也被稱為數(shù)值對象。傳輸對象是一個具有 getter/setter 方法的簡單的 POJO 類,它是可序列化的,所以它可以通過網(wǎng)絡(luò)傳輸。它沒有任何的行為。服務(wù)器端的業(yè)務(wù)類通常從數(shù)據(jù)庫讀取數(shù)據(jù),然后填充 POJO,并把它發(fā)送到客戶端或按值傳遞它。對于客戶端,傳輸對象是只讀的??蛻舳丝梢詣?chuàng)建自己的傳輸對象,并把它傳遞給服務(wù)器,以便一次性更新數(shù)據(jù)庫中的數(shù)值。以下是這種設(shè)計模式的實(shí)體。

  • 業(yè)務(wù)對象(Business Object) - 為傳輸對象填充數(shù)據(jù)的業(yè)務(wù)服務(wù)。

  • 傳輸對象(Transfer Object) - 簡單的 POJO,只有設(shè)置/獲取屬性的方法。

  • 客戶端(Client) - 客戶端可以發(fā)送請求或者發(fā)送傳輸對象到業(yè)務(wù)對象。

實(shí)現(xiàn)

我們將創(chuàng)建一個作為業(yè)務(wù)對象的 StudentBO 和作為傳輸對象的 StudentVO,它們都代表了我們的實(shí)體。

TransferObjectPatternDemo,我們的演示類在這里是作為一個客戶端,將使用 StudentBO 和  Student 來演示傳輸對象設(shè)計模式。

transferobject_pattern_uml_diagram.jpg

步驟 1

創(chuàng)建傳輸對象。

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;
   }
}

步驟 2

創(chuàng)建業(yè)務(wù)對象。

StudentBO.java

import java.util.ArrayList;
import java.util.List;

public class StudentBO {
	
   //列表是當(dāng)作一個數(shù)據(jù)庫
   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");
   }

   //從數(shù)據(jù)庫中檢索學(xué)生名單
   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");
   }
}

步驟 3

使用 StudentBO 來演示傳輸對象設(shè)計模式。

TransferObjectPatternDemo.java

public class TransferObjectPatternDemo {
   public static void main(String[] args) {
      StudentBO studentBusinessObject = new StudentBO();

      //輸出所有的學(xué)生
      for (StudentVO student : studentBusinessObject.getAllStudents()) {
         System.out.println("Student: [RollNo : "
         +student.getRollNo()+", Name : "+student.getName()+" ]");
      }

      //更新學(xué)生
      StudentVO student =studentBusinessObject.getAllStudents().get(0);
      student.setName("Michael");
      studentBusinessObject.updateStudent(student);

      //獲取學(xué)生
      studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : "
      +student.getRollNo()+", Name : "+student.getName()+" ]");
   }
}

步驟 4

驗(yàn)證輸出。

Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]