Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5bc61c8

Browse files
siavashsoleymanisiavashsoleymaniohbus
authored
iluwatar#1569 DTO pattern implementation using Enums (iluwatar#1570)
* iluwatar#1569 DTO pattern implemented using Enums * iluwatar#1569 DTO pattern implemented using Enums * iluwatar#1569 adding some java docs * iluwatar#1569 some changes in java doc and code style * iluwatar#1569 some changes in java doc and code style * iluwatar#1569 some changes in java doc and code style * iluwatar#1569 some changes in java doc and code style * iluwatar#1569 adding suggested extra line * iluwatar#1569 license added to pom.xml * iluwatar#1569 more checkstyle problems resolved * iluwatar#1569 more checkstyle problems resolved * iluwatar#1569 more checkstyle problems resolved Co-authored-by: siavashsoleymani <siavash.soleimani@snapp.cab> Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
1 parent a94615a commit 5bc61c8

File tree

10 files changed

+774
-0
lines changed

10 files changed

+774
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
layout: pattern
3+
title: Data Transfer Object
4+
folder: data-transfer-object
5+
permalink: /patterns/data-transfer-object/
6+
categories: Architectural
7+
tags:
8+
- Performance
9+
---
10+
11+
## Intent
12+
13+
Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to
14+
remote server.
15+
16+
## Explanation
17+
18+
Real world example
19+
20+
> We need to fetch information about customers from remote database. Instead of querying the
21+
> attributes one at a time, we use DTOs to transfer all the relevant attributes in a single shot.
22+
23+
In plain words
24+
25+
> Using DTO relevant information can be fetched with a single backend query.
26+
27+
Wikipedia says
28+
29+
> In the field of programming a data transfer object (DTO) is an object that carries data between
30+
> processes. The motivation for its use is that communication between processes is usually done
31+
> resorting to remote interfaces (e.g. web services), where each call is an expensive operation.
32+
> Because the majority of the cost of each call is related to the round-trip time between the client
33+
> and the server, one way of reducing the number of calls is to use an object (the DTO) that
34+
> aggregates the data that would have been transferred by the several calls, but that is served by
35+
> one call only.
36+
37+
## Class diagram
38+
39+
![alt text](./etc/dto-enum-uml.png "data-transfer-object")
40+
41+
## Applicability
42+
43+
Use the Data Transfer Object pattern when:
44+
45+
* The client is asking for multiple information. And the information is related.
46+
* When you want to boost the performance to get resources.
47+
* You want reduced number of remote calls.
48+
49+
## Credits
50+
51+
* [Design Pattern - Transfer Object Pattern](https://www.tutorialspoint.com/design_pattern/transfer_object_pattern.htm)
52+
* [Data Transfer Object](https://msdn.microsoft.com/en-us/library/ff649585.aspx)
53+
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)
54+
* [Patterns of Enterprise Application Architecture](https://www.amazon.com/gp/product/0321127420/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321127420&linkCode=as2&tag=javadesignpat-20&linkId=014237a67c9d46f384b35e10151956bd)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
@startuml
2+
package com.iluwatar.datatransferenum {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
}
8+
class Product {
9+
- cost : Double
10+
- id : Long
11+
- name : String
12+
- price : Double
13+
- supplier : String
14+
+ Product()
15+
+ getCost() : Double
16+
+ getId() : Long
17+
+ getName() : String
18+
+ getPrice() : Double
19+
+ getSupplier() : String
20+
+ setCost(cost : Double) : Product
21+
+ setId(id : Long) : Product
22+
+ setName(name : String) : Product
23+
+ setPrice(price : Double) : Product
24+
+ setSupplier(supplier : String) : Product
25+
+ toString() : String
26+
}
27+
enum ProductDTO {
28+
+ valueOf(name : String) : ProductDTO {static}
29+
+ values() : ProductDTO[] {static}
30+
}
31+
-interface Cost {
32+
+ getCost() : Double {abstract}
33+
}
34+
-interface Id {
35+
+ getId() : Long {abstract}
36+
}
37+
-interface Name {
38+
+ getName() : String {abstract}
39+
}
40+
-interface Price {
41+
+ getPrice() : Double {abstract}
42+
}
43+
enum Request {
44+
+ valueOf(name : String) : Request {static}
45+
+ values() : Request[] {static}
46+
}
47+
class Create {
48+
- cost : Double
49+
- name : String
50+
- price : Double
51+
- supplier : String
52+
+ Create()
53+
+ getCost() : Double
54+
+ getName() : String
55+
+ getPrice() : Double
56+
+ getSupplier() : String
57+
+ setCost(cost : Double) : Create
58+
+ setName(name : String) : Create
59+
+ setPrice(price : Double) : Create
60+
+ setSupplier(supplier : String) : Create
61+
}
62+
enum Response {
63+
+ valueOf(name : String) : Response {static}
64+
+ values() : Response[] {static}
65+
}
66+
class Private {
67+
- cost : Double
68+
- id : Long
69+
- name : String
70+
- price : Double
71+
+ Private()
72+
+ getCost() : Double
73+
+ getId() : Long
74+
+ getName() : String
75+
+ getPrice() : Double
76+
+ setCost(cost : Double) : Private
77+
+ setId(id : Long) : Private
78+
+ setName(name : String) : Private
79+
+ setPrice(price : Double) : Private
80+
+ toString() : String
81+
}
82+
class Public {
83+
- id : Long
84+
- name : String
85+
- price : Double
86+
+ Public()
87+
+ getId() : Long
88+
+ getName() : String
89+
+ getPrice() : Double
90+
+ setId(id : Long) : Public
91+
+ setName(name : String) : Public
92+
+ setPrice(price : Double) : Public
93+
+ toString() : String
94+
}
95+
-interface Supplier {
96+
+ getSupplier() : String {abstract}
97+
}
98+
class ProductResource {
99+
- products : List<Product>
100+
+ ProductResource(products : List<Product>)
101+
+ getAllProductsForAdmin() : List<Private>
102+
+ getAllProductsForCustomer() : List<Public>
103+
+ getProducts() : List<Product>
104+
+ save(createProductDTO : Create)
105+
}
106+
}
107+
Create ..+ Request
108+
Request ..+ ProductDTO
109+
Private ..+ Response
110+
Supplier ..+ ProductDTO
111+
Name ..+ ProductDTO
112+
ProductResource --> "-products" Product
113+
Public ..+ Response
114+
Id ..+ ProductDTO
115+
Price ..+ ProductDTO
116+
Response ..+ ProductDTO
117+
Cost ..+ ProductDTO
118+
Create ..|> Name
119+
Create ..|> Price
120+
Create ..|> Cost
121+
Create ..|> Supplier
122+
Private ..|> Id
123+
Private ..|> Name
124+
Private ..|> Price
125+
Private ..|> Cost
126+
Public ..|> Id
127+
Public ..|> Name
128+
Public ..|> Price
129+
@enduml
Loading
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.24.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>data-transfer-object-enum-impl</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-assembly-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<configuration>
50+
<archive>
51+
<manifest>
52+
<mainClass>com.iluwatar.datatransferenum.App</mainClass>
53+
</manifest>
54+
</archive>
55+
</configuration>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.iluwatar.datatransferenum;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/**
10+
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
11+
* serve related information together to avoid multiple call for each piece of information.
12+
*
13+
* <p>In this example, ({@link App}) as as product details consumer i.e. client to
14+
* request for product details to server.
15+
*
16+
* <p>productResource ({@link ProductResource}) act as server to serve product information. And
17+
* The productDto ({@link ProductDto} is data transfer object to share product information.
18+
*/
19+
public class App {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
22+
23+
/**
24+
* Method as act client and request to server for details.
25+
*
26+
* @param args program argument.
27+
*/
28+
public static void main(String[] args) {
29+
Product tv =
30+
new Product().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
31+
Product microwave =
32+
new Product().setId(2L).setName("microwave").setSupplier("Delonghi").setPrice(1000D)
33+
.setCost(1090D);
34+
Product refrigerator =
35+
new Product().setId(3L).setName("refrigerator").setSupplier("Botsch").setPrice(1000D)
36+
.setCost(1090D);
37+
Product airConditioner =
38+
new Product().setId(4L).setName("airConditioner").setSupplier("LG").setPrice(1000D)
39+
.setCost(1090D);
40+
List<Product> products =
41+
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
42+
ProductResource productResource = new ProductResource(products);
43+
44+
LOGGER.info("####### List of products including sensitive data just for admins: \n {}",
45+
Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
46+
LOGGER.info("####### List of products for customers: \n {}",
47+
Arrays.toString(productResource.getAllProductsForCustomer().toArray()));
48+
49+
LOGGER.info("####### Going to save Sony PS5 ...");
50+
ProductDto.Request.Create createProductRequestDto = new ProductDto.Request.Create()
51+
.setName("PS5")
52+
.setCost(1000D)
53+
.setPrice(1220D)
54+
.setSupplier("Sony");
55+
productResource.save(createProductRequestDto);
56+
LOGGER.info("####### List of products after adding PS5: {}",
57+
Arrays.toString(productResource.getProducts().toArray()));
58+
}
59+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.iluwatar.datatransferenum;
2+
3+
/**
4+
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
5+
*/
6+
public final class Product {
7+
private Long id;
8+
private String name;
9+
private Double price;
10+
private Double cost;
11+
private String supplier;
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param id product id
17+
* @param name product name
18+
* @param price product price
19+
* @param cost product cost
20+
* @param supplier product supplier
21+
*/
22+
public Product(Long id, String name, Double price, Double cost, String supplier) {
23+
this.id = id;
24+
this.name = name;
25+
this.price = price;
26+
this.cost = cost;
27+
this.supplier = supplier;
28+
}
29+
30+
/**
31+
* Constructor.
32+
*/
33+
public Product() {
34+
}
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public Product setId(Long id) {
41+
this.id = id;
42+
return this;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public Product setName(String name) {
50+
this.name = name;
51+
return this;
52+
}
53+
54+
public Double getPrice() {
55+
return price;
56+
}
57+
58+
public Product setPrice(Double price) {
59+
this.price = price;
60+
return this;
61+
}
62+
63+
public Double getCost() {
64+
return cost;
65+
}
66+
67+
public Product setCost(Double cost) {
68+
this.cost = cost;
69+
return this;
70+
}
71+
72+
public String getSupplier() {
73+
return supplier;
74+
}
75+
76+
public Product setSupplier(String supplier) {
77+
this.supplier = supplier;
78+
return this;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "Product{"
84+
+ "id=" + id
85+
+ ", name='" + name + '\''
86+
+ ", price=" + price
87+
+ ", cost=" + cost
88+
+ ", supplier='" + supplier + '\''
89+
+ '}';
90+
}
91+
}

0 commit comments

Comments
 (0)