IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

三十七、传输对象模式 ( Transfer Object Pattern )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=2725

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

传输对象也被称为数值对象,没有任何行为

传输对象是一个具有 getter/setter 方法的简单的 POJO 类,它是可序列化的,所以它可以通过网络传输

服务器端的业务类通常从数据库读取数据,然后填充 POJO,并把它发送到客户端或按值传递它

对于客户端,传输对象是只读的

客户端可以创建自己的传输对象,并把它传递给服务器,以便一次性更新数据库中的数值

以下是这种设计模式的实体:

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

实现

创建一个作为业务对象的 StudentBO 和作为传输对象的 StudentVO ,它们都代表了我们的实体

TransferObjectPatternDemo ,我们的演示类在这里是作为一个客户端,将使用 StudentBOStudent 来演示传输对象设计模式

img_1.png

范例

1. 创建传输对象 StudentVO

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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. 创建业务对象 StudentBO

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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

3. 使用 StudentBO 来演示传输对象设计模式

// author: 搜云库技术团队(tech.souyunku.com)
// Copyright © 2015-2065 tech.souyunku.com. All rights reserved.

package com.souyunku.tech.gof;


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()+" ]");
   }
}

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/souyunku/tech/gof/TransferObjectPatternDemo.java
$ java  com.souyunku.tech.gof.TransferObjectPatternDemo
Student: [RollNo : 0, Name : Robert ]
Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]

干货推荐

本站推荐:精选优质专栏

附录:设计模式:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(96) 打赏



未经允许不得转载:搜云库技术团队 » 三十七、传输对象模式 ( Transfer Object Pattern )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367