Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5bc61c8

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 resolvedCo-authored-by: siavashsoleymani <siavash.soleimani@snapp.cab>Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
1 parenta94615a commit5bc61c8

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+
packagecom.iluwatar.datatransferenum {
3+
classApp {
4+
-LOGGER : Logger {static}
5+
+App()
6+
+main(args : String[]) {static}
7+
}
8+
classProduct {
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+
enumProductDTO {
28+
+valueOf(name : String) : ProductDTO {static}
29+
+values() : ProductDTO[] {static}
30+
}
31+
-interfaceCost {
32+
+getCost() :Double {abstract}
33+
}
34+
-interfaceId {
35+
+getId() :Long {abstract}
36+
}
37+
-interfaceName {
38+
+getName() :String {abstract}
39+
}
40+
-interfacePrice {
41+
+getPrice() :Double {abstract}
42+
}
43+
enumRequest {
44+
+valueOf(name : String) : Request {static}
45+
+values() : Request[] {static}
46+
}
47+
classCreate {
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+
enumResponse {
63+
+valueOf(name : String) : Response {static}
64+
+values() : Response[] {static}
65+
}
66+
classPrivate {
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+
classPublic {
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+
-interfaceSupplier {
96+
+getSupplier() :String {abstract}
97+
}
98+
classProductResource {
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
120 KB
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+
<projectxmlns: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+
packagecom.iluwatar.datatransferenum;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.List;
6+
importorg.slf4j.Logger;
7+
importorg.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+
publicclassApp {
20+
21+
privatestaticfinalLoggerLOGGER =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+
publicstaticvoidmain(String[]args) {
29+
Producttv =
30+
newProduct().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
31+
Productmicrowave =
32+
newProduct().setId(2L).setName("microwave").setSupplier("Delonghi").setPrice(1000D)
33+
.setCost(1090D);
34+
Productrefrigerator =
35+
newProduct().setId(3L).setName("refrigerator").setSupplier("Botsch").setPrice(1000D)
36+
.setCost(1090D);
37+
ProductairConditioner =
38+
newProduct().setId(4L).setName("airConditioner").setSupplier("LG").setPrice(1000D)
39+
.setCost(1090D);
40+
List<Product>products =
41+
newArrayList<>(Arrays.asList(tv,microwave,refrigerator,airConditioner));
42+
ProductResourceproductResource =newProductResource(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.CreatecreateProductRequestDto =newProductDto.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+
packagecom.iluwatar.datatransferenum;
2+
3+
/**
4+
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
5+
*/
6+
publicfinalclassProduct {
7+
privateLongid;
8+
privateStringname;
9+
privateDoubleprice;
10+
privateDoublecost;
11+
privateStringsupplier;
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+
publicProduct(Longid,Stringname,Doubleprice,Doublecost,Stringsupplier) {
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+
publicProduct() {
34+
}
35+
36+
publicLonggetId() {
37+
returnid;
38+
}
39+
40+
publicProductsetId(Longid) {
41+
this.id =id;
42+
returnthis;
43+
}
44+
45+
publicStringgetName() {
46+
returnname;
47+
}
48+
49+
publicProductsetName(Stringname) {
50+
this.name =name;
51+
returnthis;
52+
}
53+
54+
publicDoublegetPrice() {
55+
returnprice;
56+
}
57+
58+
publicProductsetPrice(Doubleprice) {
59+
this.price =price;
60+
returnthis;
61+
}
62+
63+
publicDoublegetCost() {
64+
returncost;
65+
}
66+
67+
publicProductsetCost(Doublecost) {
68+
this.cost =cost;
69+
returnthis;
70+
}
71+
72+
publicStringgetSupplier() {
73+
returnsupplier;
74+
}
75+
76+
publicProductsetSupplier(Stringsupplier) {
77+
this.supplier =supplier;
78+
returnthis;
79+
}
80+
81+
@Override
82+
publicStringtoString() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp