Java 中的数据传输对象模式:简化子系统间的数据交换
约 4 分钟
亦称
- 传输对象
- 值对象
数据传输对象设计模式的意图
数据传输对象 (DTO) 模式用于在软件应用程序子系统或层之间传输数据,尤其是在 Java 应用程序的网络调用或数据库检索的上下文中。它通过将数据聚合到单个传输中来减少方法调用的数量。
数据传输对象模式的详细解释以及实际例子
实际例子
想象一家大型公司拥有多个部门(例如,销售、人力资源和 IT),需要高效地共享员工信息。与每个部门分别查询和检索姓名、地址和角色等数据不同,他们使用快递服务将这些数据捆绑成一个单一包裹。这个包裹代表一个数据传输对象 (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) {
// Example 1: Customer DTO
var 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("All customers:");
var allCustomers = customerResource.customers();
printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Deleting customer with id {1}");
customerResource.delete(customerOne.id());
allCustomers = customerResource.customers();
printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Adding customer three}");
var customerThree = new CustomerDto("3", "Lynda", "Blair");
customerResource.save(customerThree);
allCustomers = customerResource.customers();
printCustomerDetails(allCustomers);
// Example 2: Product DTO
Product 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(
"####### List of products including sensitive data just for admins: \n {}",
Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
LOGGER.info(
"####### List of products for customers: \n {}",
Arrays.toString(productResource.getAllProductsForCustomer().toArray()));
LOGGER.info("####### Going to save Sony PS5 ...");
ProductDto.Request.Create createProductRequestDto =
new ProductDto.Request.Create()
.setName("PS5")
.setCost(1000D)
.setPrice(1220D)
.setSupplier("Sony");
productResource.save(createProductRequestDto);
LOGGER.info(
"####### List of products after adding 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 -- All customers:
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 -- Deleting customer with 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 -- Adding customer three}
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 -- ####### List of products including sensitive data just for admins:
[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 -- ####### List of products for customers:
[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 -- ####### Going to save Sony PS5 ...
11:10:51.856 [main] INFO com.iluwatar.datatransfer.App -- ####### List of products after adding 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 中 DTO 模式的实际应用
- Java 中的远程方法调用 (RMI),其中 DTO 用于在网络上传递数据。
- 企业 JavaBean (EJB),尤其是在需要将数据从 EJB 传输到客户端时。
- 各种 Web 服务框架,其中 DTO 封装请求和响应数据。
数据传输对象模式的优点和缺点
优点
- 减少网络调用,从而提高应用程序性能。
- 将客户端与服务器解耦,从而产生更模块化、更易维护的代码。
- 通过将数据聚合到单个对象中,简化了网络上的数据传输。
缺点
- 在应用程序中引入了额外的类,可能会增加复杂性。
- 可能导致冗余的数据结构,这些数据结构会镜像领域模型,可能导致同步问题。
- 可能会鼓励导致贫血领域模型的设计,其中业务逻辑与数据分离。
相关模式
- 组合实体:DTO 可用于表示组合实体,尤其是在持久性机制中。
- 外观模式:与 DTO 类似,外观模式可以将多个调用聚合到一个调用中,提高效率。
- 服务层:通常涉及使用 DTO 在服务层与其客户端之间的边界上传输数据。