Skip to content

Commit 09cee8f

Browse files
committed
iluwatar#590 add explanation for DTO
1 parent 5aacdec commit 09cee8f

File tree

5 files changed

+121
-7
lines changed

5 files changed

+121
-7
lines changed

data-transfer-object/README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,89 @@ tags:
99
---
1010

1111
## Intent
12-
Pass data with multiple attributes in one shot from client to server,
13-
to avoid multiple calls to remote server.
12+
Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to remote server.
13+
14+
## Explanation
15+
16+
Real world example
17+
18+
> 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.
19+
20+
In plain words
21+
22+
> Using DTO relevant information can be fetched with a single backend query.
23+
24+
Wikipedia says
25+
26+
> In the field of programming a data transfer object (DTO) is an object that carries data between processes. The
27+
motivation for its use is that communication between processes is usually done resorting to remote interfaces
28+
(e.g., web services), where each call is an expensive operation. Because the majority of the cost of each call is
29+
related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an
30+
object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by
31+
one call only.
32+
33+
**Programmatic Example**
34+
35+
Let's first introduce our simple customer DTO class.
36+
37+
```java
38+
public class CustomerDto {
39+
private final String id;
40+
private final String firstName;
41+
private final String lastName;
42+
43+
public CustomerDto(String id, String firstName, String lastName) {
44+
this.id = id;
45+
this.firstName = firstName;
46+
this.lastName = lastName;
47+
}
48+
49+
public String getId() {
50+
return id;
51+
}
52+
53+
public String getFirstName() {
54+
return firstName;
55+
}
56+
57+
public String getLastName() {
58+
return lastName;
59+
}
60+
}
61+
```
62+
63+
Customer resource class acts as the server for customer information.
64+
65+
```java
66+
public class CustomerResource {
67+
private List<CustomerDto> customers;
68+
69+
public CustomerResource(List<CustomerDto> customers) {
70+
this.customers = customers;
71+
}
72+
73+
public List<CustomerDto> getAllCustomers() {
74+
return customers;
75+
}
76+
77+
public void save(CustomerDto customer) {
78+
customers.add(customer);
79+
}
80+
81+
public void delete(String customerId) {
82+
customers.removeIf(customer -> customer.getId().equals(customerId));
83+
}
84+
}
85+
```
86+
87+
Now fetching customer information is easy since we have the DTOs.
88+
89+
```java
90+
var allCustomers = customerResource.getAllCustomers();
91+
allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName()));
92+
// Kelly
93+
// Alfonso
94+
```
1495

1596
## Class diagram
1697
![alt text](./etc/data-transfer-object.urm.png "data-transfer-object")

data-transfer-object/etc/data-transfer-object.ucls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<class-diagram version="1.2.0" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
33
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4-
<class id="1" language="java" name="com.iluwatar.datatransfer.CustomerClientApp" project="data-transfer-object"
4+
<class id="1" language="java" name="com.iluwatar.datatransfer.App" project="data-transfer-object"
55
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java" binary="false"
66
corner="BOTTOM_RIGHT">
77
<position height="-1" width="-1" x="145" y="93"/>

data-transfer-object/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<configuration>
4949
<archive>
5050
<manifest>
51-
<mainClass>com.iluwatar.datatransfer.CustomerClientApp</mainClass>
51+
<mainClass>com.iluwatar.datatransfer.App</mainClass>
5252
</manifest>
5353
</archive>
5454
</configuration>

data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java renamed to data-transfer-object/src/main/java/com/iluwatar/datatransfer/App.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
3333
* serve related information together to avoid multiple call for each piece of information.
3434
*
35-
* <p>In this example, ({@link CustomerClientApp}) as as customer details consumer i.e. client to
35+
* <p>In this example, ({@link App}) as as customer details consumer i.e. client to
3636
* request for customer details to server.
3737
*
3838
* <p>CustomerResource ({@link CustomerResource}) act as server to serve customer information. And
3939
* The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
4040
*/
41-
public class CustomerClientApp {
41+
public class App {
4242

43-
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.class);
43+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
4444

4545
/**
4646
* Method as act client and request to server for details.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.datatransfer;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
public class AppTest {
29+
@Test
30+
public void test() throws Exception {
31+
App.main(new String[]{});
32+
}
33+
}

0 commit comments

Comments
 (0)