From 09cee8ffa7b50f51e39eb0c6d6a94aa037229ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 18 Jul 2020 17:10:50 +0300 Subject: [PATCH] #590 add explanation for DTO --- data-transfer-object/README.md | 85 ++++++++++++++++++- .../etc/data-transfer-object.ucls | 2 +- data-transfer-object/pom.xml | 2 +- .../{CustomerClientApp.java => App.java} | 6 +- .../com/iluwatar/datatransfer/AppTest.java | 33 +++++++ 5 files changed, 121 insertions(+), 7 deletions(-) rename data-transfer-object/src/main/java/com/iluwatar/datatransfer/{CustomerClientApp.java => App.java} (93%) create mode 100644 data-transfer-object/src/test/java/com/iluwatar/datatransfer/AppTest.java diff --git a/data-transfer-object/README.md b/data-transfer-object/README.md index c32ecaff7..e9286ce03 100644 --- a/data-transfer-object/README.md +++ b/data-transfer-object/README.md @@ -9,8 +9,89 @@ tags: --- ## Intent -Pass data with multiple attributes in one shot from client to server, -to avoid multiple calls to remote server. +Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to remote server. + +## Explanation + +Real world example + +> We need to fetch information about customers from remote database. Instead of querying the attributes one at a time, we use DTOs to transfer all the relevant attributes in a single shot. + +In plain words + +> Using DTO relevant information can be fetched with a single backend query. + +Wikipedia says + +> In the field of programming a data transfer object (DTO) is an object that carries data between processes. The +motivation for its use is that communication between processes is usually done resorting to remote interfaces +(e.g., web services), where each call is an expensive operation. Because the majority of the cost of each call is +related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an +object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by +one call only. + +**Programmatic Example** + +Let's first introduce our simple customer DTO class. + +```java +public class CustomerDto { + private final String id; + private final String firstName; + private final String lastName; + + public CustomerDto(String id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public String getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } +} +``` + +Customer resource class acts as the server for customer information. + +```java +public class CustomerResource { + private List customers; + + public CustomerResource(List customers) { + this.customers = customers; + } + + public List getAllCustomers() { + return customers; + } + + public void save(CustomerDto customer) { + customers.add(customer); + } + + public void delete(String customerId) { + customers.removeIf(customer -> customer.getId().equals(customerId)); + } +} +``` + +Now fetching customer information is easy since we have the DTOs. + +```java + var allCustomers = customerResource.getAllCustomers(); + allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName())); + // Kelly + // Alfonso +``` ## Class diagram ![alt text](./etc/data-transfer-object.urm.png "data-transfer-object") diff --git a/data-transfer-object/etc/data-transfer-object.ucls b/data-transfer-object/etc/data-transfer-object.ucls index 15f777aad..66236f32d 100644 --- a/data-transfer-object/etc/data-transfer-object.ucls +++ b/data-transfer-object/etc/data-transfer-object.ucls @@ -1,7 +1,7 @@ - diff --git a/data-transfer-object/pom.xml b/data-transfer-object/pom.xml index 5889daba8..459b1ab1e 100644 --- a/data-transfer-object/pom.xml +++ b/data-transfer-object/pom.xml @@ -48,7 +48,7 @@ - com.iluwatar.datatransfer.CustomerClientApp + com.iluwatar.datatransfer.App diff --git a/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java b/data-transfer-object/src/main/java/com/iluwatar/datatransfer/App.java similarity index 93% rename from data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java rename to data-transfer-object/src/main/java/com/iluwatar/datatransfer/App.java index ebc44dc57..b8630d4ea 100644 --- a/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java +++ b/data-transfer-object/src/main/java/com/iluwatar/datatransfer/App.java @@ -32,15 +32,15 @@ import org.slf4j.LoggerFactory; * The Data Transfer Object pattern is a design pattern in which an data transfer object is used to * serve related information together to avoid multiple call for each piece of information. * - *

In this example, ({@link CustomerClientApp}) as as customer details consumer i.e. client to + *

In this example, ({@link App}) as as customer details consumer i.e. client to * request for customer details to server. * *

CustomerResource ({@link CustomerResource}) act as server to serve customer information. And * The CustomerDto ({@link CustomerDto} is data transfer object to share customer information. */ -public class CustomerClientApp { +public class App { - private static final Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.class); + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Method as act client and request to server for details. diff --git a/data-transfer-object/src/test/java/com/iluwatar/datatransfer/AppTest.java b/data-transfer-object/src/test/java/com/iluwatar/datatransfer/AppTest.java new file mode 100644 index 000000000..3a58d0c54 --- /dev/null +++ b/data-transfer-object/src/test/java/com/iluwatar/datatransfer/AppTest.java @@ -0,0 +1,33 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.datatransfer; + +import org.junit.jupiter.api.Test; + +public class AppTest { + @Test + public void test() throws Exception { + App.main(new String[]{}); + } +}