本文主要是介绍【Java设计模式】数据传输对象模式:简化子系统间的数据交换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 【Java设计模式】数据传输对象模式:简化子系统间的数据交换
- 一、概述
- 二、详细解释及实际示例
- 三、Java中DTO模式的编程示例
- 四、何时在Java中使用数据传输对象模式
- 五、数据传输对象模式在Java中的实际应用
- 六、数据传输对象模式的优点和权衡
- 七、源码下载
【Java设计模式】数据传输对象模式:简化子系统间的数据交换
一、概述
数据传输对象(DTO)模式用于在软件应用程序的子系统或层之间传输数据,特别是在Java应用程序中的网络调用或数据库检索的上下文中。它通过将数据聚合在单个传输中,减少了方法调用的次数。
二、详细解释及实际示例
- 实际示例:
- 想象一个大型公司,有几个部门(如销售、人力资源和信息技术)需要高效地共享员工信息。与其每个部门单独查询和检索数据,如姓名、地址和角色,他们使用快递服务将这些数据捆绑成一个单独的包裹。这个代表数据传输对象(DTO)的包裹允许部门轻松接收和处理全面的员工数据,而无需进行多次请求。这简化了数据处理,减少了通信开销,并确保了整个公司的标准化格式。
- 通俗解释:
- 使用DTO,可以通过单个后端查询获取相关信息。
- 维基百科解释:
- 在编程领域,数据传输对象(DTO)是在进程之间携带数据的对象。使用它的动机是,进程之间的通信通常依赖于远程接口(如Web服务),其中每个调用都是昂贵的操作。由于每个调用的大部分成本与客户端和服务器之间的往返时间相关,减少调用次数的一种方法是使用一个对象(DTO),该对象聚合了原本需要通过多次调用传输的数据,但仅通过一次调用提供服务。
三、Java中DTO模式的编程示例
首先介绍我们简单的CustomerDto
记录。
public record CustomerDto(String id, String firstName, String lastName) {}
CustomerResource
记录充当客户信息的服务器。
public record CustomerResource(List<CustomerDto> customers) {public void save(CustomerDto customer) {customers.add(customer);}public void delete(String customerId) {customers.removeIf(customer -> customer.id().equals(customerId));}
}
现在,由于我们有DTO,获取客户信息变得很容易。第二个示例类似地使用了ProductDTO
。
public class App {public static void main(String[] args) {// 示例 1:客户DTOvar customerOne = new CustomerDto("1", "Kelly", "Brown");var customerTwo = new CustomerDto("2", "Alfonso", "Bass");var customers = new ArrayList<>(List.of(customerOne, customerTwo));var customerResource = new CustomerResource(customers);LOGGER.info("所有客户:");var allCustomers = customerResource.customers();printCustomerDetails(allCustomers);LOGGER.info("----------------------------------------------------------");LOGGER.info("删除ID为{1}的客户");customerResource.delete(customerOne.id());allCustomers = customerResource.customers();printCustomerDetails(allCustomers);LOGGER.info("----------------------------------------------------------");LOGGER.info("添加客户三");var customerThree = new CustomerDto("3", "Lynda", "Blair");customerResource.save(customerThree);allCustomers = customerResource.customers();printCustomerDetails(allCustomers);// 示例 2:产品DTOProduct tv = Product.builder().id(1L).name("TV").supplier("Sony").price(1000D).cost(1090D).build();Product microwave =Product.builder().id(2L).name("microwave").supplier("Delonghi").price(1000D).cost(1090D).build();Product refrigerator =Product.builder().id(3L).name("refrigerator").supplier("Botsch").price(1000D).cost(1090D).build();Product airConditioner =Product.builder().id(4L).name("airConditioner").supplier("LG").price(1000D).cost(1090D).build();List<Product> products =new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));ProductResource productResource = new ProductResource(products);LOGGER.info("####### 包含敏感数据的产品列表,仅供管理员使用: \n {}",Arrays.toString(productResource.getAllProductsForAdmin().toArray()));LOGGER.info("####### 供客户使用的产品列表: \n {}",Arrays.toString(productResource.getAllProductsForCustomer().toArray()));LOGGER.info("####### 将要保存索尼PS5...");ProductDto.Request.Create createProductRequestDto =new ProductDto.Request.Create().setName("PS5").setCost(1000D).setPrice(1220D).setSupplier("Sony");productResource.save(createProductRequestDto);LOGGER.info("####### 添加PS5后的产品列表: {}",Arrays.toString(productResource.products().toArray()));}private static void printCustomerDetails(List<CustomerDto> allCustomers) {allCustomers.forEach(customer -> LOGGER.info(customer.firstName()));}
}
控制台输出:
11:10:51.838 [main] INFO com.iluwatar.datatransfer.App -- 所有客户:
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- Kelly
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- Alfonso
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- ----------------------------------------------------------
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- 删除ID为{1}的客户
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- Alfonso
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- ----------------------------------------------------------
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- 添加客户三
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- Alfonso
11:10:51.840 [main] INFO com.iluwatar.datatransfer.App -- Lynda
11:10:51.848 [main] INFO com.iluwatar.datatransfer.App -- ####### 包含敏感数据的产品列表,仅供管理员使用: [Private{id=1, name='TV', price=1000.0, cost=1090.0}, Private{id=2, name='microwave', price=1000.0, cost=1090.0}, Private{id=3, name='refrigerator', price=1000.0, cost=1090.0}, Private{id=4, name='airConditioner', price=1000.0, cost=1090.0}]
11:10:51.852 [main] INFO com.iluwatar.datatransfer.App -- ####### 供客户使用的产品列表: [Public{id=1, name='TV', price=1000.0}, Public{id=2, name='microwave', price=1000.0}, Public{id=3, name='refrigerator', price=1000.0}, Public{id=4, name='airConditioner', price=1000.0}]
11:10:51.852 [main] INFO com.iluwatar.datatransfer.App -- ####### 将要保存索尼PS5...
11:10:51.856 [main] INFO com.iluwatar.datatransfer.App -- ####### 添加PS5后的产品列表: [Product{id=1, name='TV', price=1000.0, cost=1090.0, supplier='Sony'}, Product{id=2, name='microwave', price=1000.0, cost=1090.0, supplier='Delonghi'}, Product{id=3, name='refrigerator', price=1000.0, cost=1090.0, supplier='Botsch'}, Product{id=4, name='airConditioner', price=1000.0, cost=1090.0, supplier='LG'}, Product{id=5, name='PS5', price=1220.0, cost=1000.0, supplier='Sony'}]
四、何时在Java中使用数据传输对象模式
在以下情况下使用数据传输对象模式:
- 当需要通过减少调用次数来优化网络流量时,特别是在客户端 - 服务器架构中。
- 在偏好批量处理数据而不是单独处理数据的场景中。
- 在使用远程接口时,将数据传输封装在可序列化对象中,以便轻松传输。
五、数据传输对象模式在Java中的实际应用
- Java中的远程方法调用(RMI),其中DTO用于跨网络传递数据。
- 企业JavaBeans(EJB),特别是当数据需要从EJB传输到客户端时。
- 各种Web服务框架,其中DTO封装请求和响应数据。
六、数据传输对象模式的优点和权衡
优点:
- 减少网络调用,从而提高应用程序性能。
- 将客户端与服务器解耦,导致更模块化和可维护的代码。
- 通过将数据聚合到单个对象中,简化了网络上的数据传输。
权衡:
- 向应用程序引入了额外的类,这可能会增加复杂性。
- 可能导致冗余的数据结构,反映域模型,可能导致同步问题。
- 可能鼓励导致贫血域模型的设计,其中业务逻辑与数据分离。
七、源码下载
数据传输对象模式示例代码下载
这篇关于【Java设计模式】数据传输对象模式:简化子系统间的数据交换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!