📌  相关文章
📜  Java中的传输对象模式

📅  最后修改于: 2022-05-13 01:54:45.190000             🧑  作者: Mango

Java中的传输对象模式

当我们想一次性将具有多个属性的数据从客户端传递到服务器时使用它。 Transfer Object 是一个简单的 POJO 类,具有 getter/setter 方法并被序列化,以便它可以通过网络传输。 Server Side 业务类通常从数据库中获取数据并填充 POJO 并将其发送给客户端或按值传递。对于客户端,传输对象是只读的。客户端可以创建自己的传输对象并将其传递给服务器以更新 数据库一键搞定。

以下是此类设计模式的实体:

Transfer ObjectSimple POJO having methods to set/get attributes only
 Business ObjectFills the Transfer Object with data
ClientEither requests or sends the Transfer Object to Business Object

方法:

  • 步骤 1:创建传输对象
  • 第 2 步:创建业务对象。
  • 第 3 步:使用 StudentBO 演示传输对象设计模式
  • 第 4 步:验证输出。

程序:

步骤 1:创建传输对象

例子

Java
// Transfer Object Pattern - Design Pattern
 
// Step 1
// Creating a Transfer Object
// randomly be named it 'StudentVO.java'
 
// Class StudentVO
public class StudentVO {
   
  // Member variables of class
   private String name;
   private int rollNo;
   
  // Creating a constructor of above class
  StudentVO(String name, int rollNo) {
     
    // This keyword for assignment
    // to same memory block created
    // for every nae and roll number of student
    this.name = name;
      this.rollNo = rollNo;
   }
   
  // Getting name of student
   public String getName() {
      return name;
   }
   
  // Setting name of Student
   public void setName(String name) {
      this.name = name;
   }
   
  // Getting roll number of student
   public int getRollNo() {
      return rollNo;
   }
   
  // Setting roll number of student
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
}


Java
// Transfer Object Pattern - Design Pattern
 
// Step 2
// Creating a Busines object
// randomly be named it 'StudentBO.java'
 
// Importing List and ArrayList classes of
// java.util package
import java.util.ArrayList;
import java.util.List;
 
// Class StudentBO
public class StudentBO {
     
   // List is working as a database
   List students;
   public StudentBO() {
  
      students = new ArrayList();
      
     // Adding custom inputs
      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");
   }
   
   //retrieve list of students from the database
   public List 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");
   }
}


Java
// Transfer Object Pattern - Design Pattern
// Step 3
 
// Use the StudentBO to demonstrate Transfer Object Design Pattern
// randomly be named it 'TransferObjectPatternDemo.java'
 
public class TransferObjectPatternDemo {
   
  // Main driver method
   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);
      
     // Custom input 
     student.setName("Michael");
      studentBusinessObject.updateStudent(student);
       
     // Getting the student
      student = studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
   }
}


步骤 2:创建业务对象

例子

Java

// Transfer Object Pattern - Design Pattern
 
// Step 2
// Creating a Busines object
// randomly be named it 'StudentBO.java'
 
// Importing List and ArrayList classes of
// java.util package
import java.util.ArrayList;
import java.util.List;
 
// Class StudentBO
public class StudentBO {
     
   // List is working as a database
   List students;
   public StudentBO() {
  
      students = new ArrayList();
      
     // Adding custom inputs
      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");
   }
   
   //retrieve list of students from the database
   public List 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 演示传输对象设计模式

实现: List 在此处充当 DB,如演示传输对象设计模式中所示。

例子

Java

// Transfer Object Pattern - Design Pattern
// Step 3
 
// Use the StudentBO to demonstrate Transfer Object Design Pattern
// randomly be named it 'TransferObjectPatternDemo.java'
 
public class TransferObjectPatternDemo {
   
  // Main driver method
   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);
      
     // Custom input 
     student.setName("Michael");
      studentBusinessObject.updateStudent(student);
       
     // Getting the student
      student = studentBusinessObject.getStudent(0);
      System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
   }
}

步骤 4:验证输出

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