数据传输对象设计模式是一种经常使用的设计模式。它主要用于从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。
目的
从客户端到服务器一次性传递具有多个属性的数据,以避免多次调用远程服务器。
问题
您希望跨层传输多个数据元素。
动机
您希望客户端访问其他层中的组件以检索和更新数据。
您希望减少网络上的远程请求。
您希望避免由流量高的聊天应用程序而导致的网络性能下降
解决方案
使用传输对象跨层传输多个数据元素。
说明
维基百科说
数据传输对象是在进程之间传输数据的对象。其使用的动机是进程之间的通信通常采用远程接口(例如,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类是客户详细信息使用者,即客户端向服务器请求客户详细信息。
<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>
结果
适用场景