Java Transfer Object Pattern(傳輸對象模式)

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

  • 業務對象(Business Object) - 為傳輸對象填充數據的業務服務。
  • 傳輸對象(Transfer Object) - 簡單的 POJO,只有設置/獲取屬性的方法。
  • 客戶端(Client) - 客戶端可以發送請求或者發送傳輸對象到業務對象。
  1. 創建傳輸對象。
/**
 * 1. 創建傳輸對象。
 * @author mazaiting
 */
public class StudentVO {
    private String name;
    private int rollNo;
    public 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;
    }   
}
  1. 創建業務對象。
/**
 * 2. 創建業務對象。
 * @author mazaiting
 */
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");
    }
    
}
  1. 使用 StudentBO 來演示傳輸對象設計模式。
/**
 * 3. 使用 StudentBO 來演示傳輸對象設計模式。
 * @author mazaiting
 */
public class Client {
    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()+" ]");
        
    }
}
  1. 打印結果
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容