内容简介:数据传输对象设计模式是一种经常使用的设计模式。它主要用于从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。
数据传输对象 设计模式 是一种经常使用的设计模式。它主要用于从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。
目的
从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。
问题
您希望跨层传输多个数据元素。
动机
您希望客户端访问其他层中的组件以检索和更新数据。
您希望减少网络上的远程请求。
您希望避免由流量高的聊天应用程序而导致的网络性能下降
解决方案
使用传输对象跨层传输多个数据元素。
说明
- 数据传输对象模式是一种设计模式,数据传输对象将相关信息一起提供,以避免对每条信息进行多次调用。
- Transfer Object是一个简单的POJO类,具有getter / setter方法,并且是可序列化的,因此可以通过网络传输。它没有任何行为。
- 服务器端业务类通常从数据库中提取数据并填充POJO并将其发送到客户端或按值传递。对于客户端,传输对象是只读的。
- 客户端可以创建自己的传输对象并将其传递给服务器,以便一次性更新数据库中的值。
维基百科说
数据传输对象是在进程之间传输数据的对象。其使用的动机是进程之间的通信通常采用远程接口(例如,Web服务),其中每个调用都是代价不菲的操作。[因为每个调用的大部分成本都与客户端和服务器之间的往返时间有关,减少调用数量的一种方法是使用一个对象(DTO)它聚集了几个调用可能传输的数据,但只由一个调用提供服务。
结构
让我们使用UML类图来展示解决方案的基本结构,本节中的UML序列图介绍了解决方案的动态机制。
下面是表示数据传输对象设计模式的关系的类图和序列图。
类图
序列图
执行
第1步: CustomerDto是数据传输对象POJO。而不是向客户端发送个人信息。我们可以在一个POJO类中一起发送相关信息。请注意,DTO中没有任何业务逻辑。
<b>public</b> <b>class</b> CustomerDto {
<b>private</b> <b>final</b> String id;
<b>private</b> <b>final</b> String firstName;
<b>private</b> <b>final</b> String lastName;
/ **
* @param id customer id
* @param firstName customer first name
* @param lastName customer name name
/ / <b>public</b> CustomerDto( String id, String firstName, String lastName){
<b>this</b> 。id = id;
这个。firstName = firstName;
这个。lastName = lastName;
}
<b>public</b> String getId(){
<b>return</b> id;
}
<b>public</b> String getFirstName(){
<b>return</b> firstName;
}
<b>public</b> String getLastName(){
<b>return</b> lastName;
}
}
第2步: 创建一个服务客户信息的资源类。该类充当演示中的服务器。客户详细信息在哪儿?
<b>public</b> <b>class</b> CustomerResource {
<b>private</b> List < CustomerDto > customers;
<font><i>/**
* @param customers initialize resource with existing customers. Act as database.
*/</i></font><font>
<b>public</b> CustomerResource(List < CustomerDto > customers) {
<b>this</b>.customers = customers;
}
</font><font><i>/**
* @return : all customers in list.
*/</i></font><font>
<b>public</b> List < CustomerDto > getAllCustomers() {
<b>return</b> customers;
}
</font><font><i>/**
* @param customer save new customer to list.
*/</i></font><font>
<b>public</b> <b>void</b> save(CustomerDto customer) {
customers.add(customer);
}
</font><font><i>/**
* @param customerId delete customer with id {@code customerId}
*/</i></font><font>
<b>public</b> <b>void</b> delete(String customerId) {
customers.removeIf(customer - > customer.getId().equals(customerId));
}
}
</font>
第3步: CustomerClientApp类是客户详细信息使用者,即客户端向服务器请求客户详细信息。
- CustomerResource充当服务器来提供客户信息。
- customerdto是用于共享客户信息的数据传输对象。
<b>public</b> <b>class</b> CustomerClientApp {
<b>private</b> <b>static</b> <b>final</b> Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.<b>class</b>);
<font><i>/**
* Method as act client and request to server for details.
*
* @param args program argument.
*/</i></font><font>
<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
List < CustomerDto > customers = <b>new</b> ArrayList < > ();
CustomerDto customerOne = <b>new</b> CustomerDto(</font><font>"1"</font><font>, </font><font>"Kelly"</font><font>, </font><font>"Brown"</font><font>);
CustomerDto customerTwo = <b>new</b> CustomerDto(</font><font>"2"</font><font>, </font><font>"Alfonso"</font><font>, </font><font>"Bass"</font><font>);
customers.add(customerOne);
customers.add(customerTwo);
CustomerResource customerResource = <b>new</b> CustomerResource(customers);
LOGGER.info(</font><font>"All customers:-"</font><font>);
List < CustomerDto > allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
LOGGER.info(</font><font>"----------------------------------------------------------"</font><font>);
LOGGER.info(</font><font>"Deleting customer with id {1}"</font><font>);
customerResource.delete(customerOne.getId());
allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
LOGGER.info(</font><font>"----------------------------------------------------------"</font><font>);
LOGGER.info(</font><font>"Adding customer three}"</font><font>);
CustomerDto customerThree = <b>new</b> CustomerDto(</font><font>"3"</font><font>, </font><font>"Lynda"</font><font>, </font><font>"Blair"</font><font>);
customerResource.save(customerThree);
allCustomers = customerResource.getAllCustomers();
printCustomerDetails(allCustomers);
}
<b>private</b> <b>static</b> <b>void</b> printCustomerDetails(List < CustomerDto > allCustomers) {
allCustomers.forEach(customer - > LOGGER.info(customer.getFirstName()));
}
}
</font>
结果
- 减少网络流量
- 简化远程对象和远程接口
- 在更少的远程调用中传输更多数据
- 减少代码重复
- 引入陈旧的转移对象
- 由于同步和版本控制而增加了复杂性。
适用场景
- 客户要求提供多种信息。而且信息是相关的。
- 当你想提高性能以获取资源时。
- 您希望减少远程呼叫的数量。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 大规模数据传输,知易行难 — 数据传输与 ETL 平台的架构演进 原 荐
- MySQL数据库字符编码总结--数据传输编码
- 东进量子加密传输网关:为数据传输套上“金钟罩”
- Koa 中实现 chunked 数据传输
- Debug Tensorflow PS的数据传输
- 中央企业“三重一大”数据传输安全的重要步骤
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Structures and Algorithms with JavaScript
Michael McMillan / O'Reilly Media / 2014-2-22 / USD 28.51
If you’re using JavaScript on the server-side, you need to implement classic data structures that conventional object-oriented programs (such as C# and Java) provide. This practical book shows you how......一起来看看 《Data Structures and Algorithms with JavaScript》 这本书的介绍吧!