8000 Merge pull request #608 from gopinath-langote/master · prog012/java-design-patterns@e367e48 · GitHub
[go: up one dir, main page]

Skip to content

Commit e367e48

Browse files
authored
Merge pull request iluwatar#608 from gopinath-langote/master
iluwatar#348 Data Transfer Object design pattern
2 parents b639f36 + f9789d6 commit e367e48

File tree

10 files changed

+438
-0
lines changed

10 files changed

+438
-0
lines changed

data-transfer-object/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
- Java
9+
- KISS
10+
- YAGNI
11+
- Difficulty-Beginner
12+
---
13+
14+
## Intent
15+
Pass data with multiple attributes in one shot from client to server,
16+
to avoid multiple calls to remote server.
17+
18+
![alt text](./etc/data-transfer-object.urm.png "data-transfer-object")
19+
20+
## Applicability
21+
Use the Data Transfer Object pattern when
22+
23+
* The client is asking for multiple information. And the information is related.
24+
* When you want to boost the performance to get resources.
25+
* You want reduced number of remote calls.
26+
27+
## Credits
28+
29+
* [Design Pattern - Transfer Object Pattern](https://www.tutorialspoint.com/design_pattern/transfer_object_pattern.htm)
30+
* [Data Transfer Object](https://msdn.microsoft.com/en-us/library/ff649585.aspx)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.0" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.datatransfer.CustomerClientApp" project="data-transfer-object"
5+
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java" binary="false"
6+
corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="145" y="93"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.datatransfer.CustomerDto" project="data-transfer-object"
15+
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerDto.java" binary="false"
16+
corner="BOTTOM_RIGHT">
17+
<position height="-1" width="-1" x="386" y="329"/>
18+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
19+
sort-features="false" accessors="true" visibility="true">
20+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
21+
<operations public="true" package="true" protected="true" private="true" static="true"/>
22+
</display>
23+
</class>
24+
<class id="3" language="java" name="com.iluwatar.datatransfer.CustomerResource" project="data-transfer-object"
25+
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerResource.java" binary="false"
26+
corner="BOTTOM_RIGHT">
27+
<position height="-1" width="-1" x="677" y="93"/>
28+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
29+
sort-features="false" accessors="true" visibility="true">
30+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
31+
<operations public="true" package="true" protected="true" private="true" static="true"/>
32+
</display>
33+
</class>
34+
<association id="4">
35+
<end type="SOURCE" refId="3" navigable="false">
36+
<attribute id="5" name="customers"/>
37+
<multiplicity id="6" minimum="0" maximum="2147483647"/>
38+
</end>
39+
<end type="TARGET" refId="2" navigable="true"/>
40+
<display labels="true" multiplicity="true"/>
41+
</association>
42+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
43+
sort-features="false" accessors="true" visibility="true">
44+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
45+
<operations public="true" package="true" protected="true" private="true" static="true"/>
46+
</classifier-display>
47+
<association-display labels="true" multiplicity="true"/>
48+
</class-diagram>
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@startuml
2+
package com.iluwatar.datatransfer {
3+
class CustomerClientApp {
4+
+ CustomerClientApp()
5+
+ main(args : String[]) {static}
6+
- printCustomerDetails(allCustomers : List<CustomerDto>) {static}
7+
}
8+
class CustomerDto {
9+
- firstName : String
10+
- id : String
11+
- lastName : String
12+
+ CustomerDto(id : String, firstName : String, lastName : String)
13+
+ getFirstName() : String
14+
+ getId() : String
15+
+ getLastName() : String
16+
}
17+
class CustomerResource {
18+
- customers : List<CustomerDto>
19+
+ CustomerResource(customers : List<CustomerDto>)
20+
+ delete(customerId : String)
21+
+ getAllCustomers() : List<CustomerDto>
22+
+ save(customer : CustomerDto)
23+
}
24+
}
25+
CustomerResource --> "-customers" CustomerDto
26+
@enduml

data-transfer-object/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2016 Gopinath Langote
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 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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.17.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>data-transfer-object</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>log4j</groupId>
42+
<artifactId>log4j</artifactId>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
/**
34+
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to serve related
35+
* information together to avoid multiple call for each piece of information.
36+
* <p>
37+
* In this example, ({@link CustomerClientApp}) as as customer details consumer i.e. client to request for
38+
* customer details to server.
39+
* <p>
40+
* CustomerResource ({@link CustomerResource}) act as server to serve customer information.
41+
* And The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
42+
*/
43+
public class CustomerClientApp {
44+
45+
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.class);
46+
47+
/**
48+
* Method as act client and request to server for details.
49+
*
50+
* @param args program argument.
51+
*/
52+
public static void main(String[] args) {
53+
List<CustomerDto> customers = new ArrayList<>();
54+
CustomerDto customerOne = new CustomerDto("1", "Kelly", "Brown");
55+
CustomerDto customerTwo = new CustomerDto("2", "Alfonso", "Bass");
56+
customers.add(customerOne);
57+
customers.add(customerTwo);
58+
59+
CustomerResource customerResource = new CustomerResource(customers);
60+
61+
LOGGER.info("All customers:-");
62+
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
63+
printCustomerDetails(allCustomers);
64+
65+
LOGGER.info("----------------------------------------------------------");
66+
67+
LOGGER.info("Deleting customer with id {1}");
68+
customerResource.delete(customerOne.getId());
69+
allCustomers = customerResource.getAllCustomers();
70+
printCustomerDetails(allCustomers);
71+
72+
LOGGER.info("----------------------------------------------------------");
73+
74+
LOGGER.info("Adding customer three}");
75+
CustomerDto customerThree = new CustomerDto("3", "Lynda", "Blair");
76+
customerResource.save(customerThree);
77+
allCustomers = customerResource.getAllCustomers();
78+
printCustomerDetails(allCustomers);
79+
}
80+
81+
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
82+
allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName()));
83+
}
84+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIAB 10000 LE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
/**
28+
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to client
29+
* We can send related information together in POJO.
30+
* <p>
31+
* Dto will not have any business logic in it.
32+
*/
33+
public class CustomerDto {
34+
private final String id;
35+
private final String firstName;
36+
private final String lastName;
37+
38+
/**
39+
* @param id customer id
40+
* @param firstName customer first name
41+
* @param lastName customer last name
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+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
import java.util.List;
28+
29+
/**
30+
* The resource class which serves customer information.
31+
* This class act as server in the demo. Which has all customer details.
32+
*/
33+
public class CustomerResource {
34+
private List<CustomerDto> customers;
35+
36+
/**
37+
* @param customers initialize resource with existing customers. Act as database.
38+
*/
39+
public CustomerResource(List<CustomerDto> customers) {
40+
this.customers = customers;
41+
}
42+
43+
/**
44+
* @return : all customers in list.
45+
*/
46+
public List<CustomerDto> getAllCustomers() {
47+
return customers;
48+
}
49+
50+
/**
51+
* @param customer save new customer to list.
52+
*/
53+
public void save(CustomerDto customer) {
54+
customers.add(customer);
55+
}
56+
57+
/**
58+
* @param customerId delete customer with id {@code customerId}
59+
*/
60+
public void delete(String customerId) {
61+
customers.removeIf(customer -> customer.getId().equals(customerId));
62+
}
63+
}

0 commit comments

Comments
 (0)
0