美文网首页Java
设计模式之传输对象模式

设计模式之传输对象模式

作者: 盗梦者_56f2 | 来源:发表于2019-07-20 21:32 被阅读0次

简介

传输对象模式(Transfer Object Pattern)用于从客户端向服务器一次性传递带有多个属性的数据。传输对象也被称为数值对象。传输对象是一个具有 getter/setter 方法的简单的 POJO 类,它是可序列化的,所以它可以通过网络传输。它没有任何的行为。服务器端的业务类通常从数据库读取数据,然后填充 POJO,并把它发送到客户端或按值传递它。对于客户端,传输对象是只读的。客户端可以创建自己的传输对象,并把它传递给服务器,以便一次性更新数据库中的数值。以下是这种设计模式的实体。

  • 业务对象(Business Object) - 为传输对象填充数据的业务服务。
  • 传输对象(Transfer Object) - 简单的 POJO,只有设置/获取属性的方法。
  • 客户端(Client) - 客户端可以发送请求或者发送传输对象到业务对象。

实现

创建一个作为业务对象的 StudentBO 和作为传输对象的 StudentVO,它们都代表了我们的实体。演示类TransferObjectPatternDemo在这里是作为一个客户端,将使用 StudentBO 和 Student 来演示传输对象设计模式。


传输对象模式

代码实现

//创建传输对象。
//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;
   }
}
//创建业务对象。
//StudentBO.java
import java.util.ArrayList;
import java.util.List;
 
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");
   }
}
//使用 StudentBO 来演示传输对象设计模式。
//TransferObjectPatternDemo.java
public class TransferObjectPatternDemo {
   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()+" ]");
   }
}
//执行程序,输出结果:

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

相关文章

  • 设计模式之传输对象模式

    简介 传输对象模式(Transfer Object Pattern)用于从客户端向服务器一次性传递带有多个属性的数...

  • 设计模式之合成模式

    @(架构之路之设计模式代码) 设计模式之合成模式 欢迎关注作者简书csdn传送门 [TOC]   合成模式属于对象...

  • 《PHP设计模式大全》系列分享专栏

    php设计模式介绍之编程惯用法第1/3页 php设计模式介绍之值对象模式第1/5页 介绍php设计模式中的工厂模式...

  • 设计模式之代理模式

    设计模式之代理模式 简介 代理模式是结构型设计模式。 当无法或不想直接访问某个对象或访问某个对象存在困难时可以通过...

  • Golang 设计模式之-装饰模式

    Golang 设计模式之-装饰模式 最近在温习设计模式,虽然面向对象不是go的特长,但用go实现的设计模式,比ja...

  • 代理模式

    设计模式之代理模式 什么是代理模式? 指的事代理对象为其他对象提供代理以控制对被...

  • 传输对象模式

    传输对象模式主要作用-适用于在不同层传输数据。例如在UI层和service层传输数据的时候,可以避免在内存中不断复...

  • 传输对象模式

    这个设计模式我是没想到在实际项目中怎么应用,在网上找的资料也只是对这个设计模式进行描述,如果你知道传输对象模式在项...

  • 命令模式(Command)

    本文参考自:《JAVA设计模式》之命令模式(Command) 1. 作用 命令模式属于对象的行为模式。命令模式又称...

  • 设计模式之代理模式2

    设计模式之代理模式运用 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对...

网友评论

    本文标题:设计模式之传输对象模式

    本文链接:https://www.haomeiwen.com/subject/jdcplctx.html