Skip to content

Commit df9be78

Browse files
author
Gopinath Langote
committed
iluwatar#348 - Data Tranfer Object : customer client request customer details to server at one shot.
1 parent 229fda9 commit df9be78

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed
4.82 KB
Loading

data-transfer-object/etc/data-transfer-object.urm.puml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
@startuml
22
package com.iluwatar.datatransfer {
3+
class CustomerClientApp {
4+
+ CustomerClientApp()
5+
+ main(args : String[]) {static}
6+
- printCustomerDetails(allCustomers : List<CustomerDto>) {static}
7+
}
38
class CustomerDto {
49
- firstName : String
510
- id : String
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.ArrayList;
28+
import java.util.List;
29+
30+
/**
31+
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to serve related
32+
* information together to avoid multiple call for each piece of information.
33+
* <p>
34+
* In this example, ({@link CustomerClientApp}) as as customer details consumer i.e. client to request for
35+
* customer details to server.
36+
* <p>
37+
* CustomerResource ({@link CustomerResource}) act as server to serve customer information.
38+
* And The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
39+
*/
40+
public class CustomerClientApp {
41+
/**
42+
* Method as act client and request to server for details.
43+
*
44+
* @param args program argument.
45+
*/
46+
public static void main(String[] args) {
47+
List<CustomerDto> customers = new ArrayList<>();
48+
CustomerDto customerOne = new CustomerDto("1", "Kelly", "Brown");
49+
CustomerDto customerTwo = new CustomerDto("2", "Alfonso", "Bass");
50+
customers.add(customerOne);
51+
customers.add(customerTwo);
52+
53+
CustomerResource customerResource = new CustomerResource(customers);
54+
55+
System.out.println("All customers:-");
56+
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
57+
printCustomerDetails(allCustomers);
58+
59+
System.out.println("----------------------------------------------------------");
60+
61+
System.out.println("Deleting customer with id {1}");
62+
customerResource.delete(customerOne.getId());
63+
allCustomers = customerResource.getAllCustomers();
64+
printCustomerDetails(allCustomers);
65+
66+
System.out.println("----------------------------------------------------------");
67+
68+
System.out.println("Adding customer three}");
69+
CustomerDto customerThree = new CustomerDto("3", "Lynda", "Blair");
70+
customerResource.save(customerThree);
71+
allCustomers = customerResource.getAllCustomers();
72+
printCustomerDetails(allCustomers);
73+
}
74+
75+
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
76+
allCustomers.forEach(customer -> System.out.println(customer.getFirstName()));
77+
}
78+
}

data-transfer-object/src/test/java/com/iluwatar/datatransfer/CustomerResourceTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
public class CustomerResourceTest {
3838
@Test
3939
public void shouldGetAllCustomers() {
40-
CustomerDto customer = new CustomerDto("1", "David", "Roy");
40+
CustomerDto customer = new CustomerDto("1", "Melody", "Yates");
4141
List<CustomerDto> customers = new ArrayList<>();
4242
customers.add(customer);
4343

@@ -47,26 +47,26 @@ public void shouldGetAllCustomers() {
4747

4848
assertEquals(allCustomers.size(), 1);
4949
assertEquals(allCustomers.get(0).getId(), "1");
50-
assertEquals(allCustomers.get(0).getFirstName(), "David");
51-
assertEquals(allCustomers.get(0).getLastName(), "Roy");
50+
assertEquals(allCustomers.get(0).getFirstName(), "Melody");
51+
assertEquals(allCustomers.get(0).getLastName(), "Yates");
5252
}
5353

5454
@Test
5555
public void shouldSaveCustomer() {
56-
CustomerDto customer = new CustomerDto("1", "David", "Roy");
56+
CustomerDto customer = new CustomerDto("1", "Rita", "Reynolds");
5757
CustomerResource customerResource = new CustomerResource(new ArrayList<>());
5858

5959
customerResource.save(customer);
6060

6161
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
6262
assertEquals(allCustomers.get(0).getId(), "1");
63-
assertEquals(allCustomers.get(0).getFirstName(), "David");
64-
assertEquals(allCustomers.get(0).getLastName(), "Roy");
63+
assertEquals(allCustomers.get(0).getFirstName(), "Rita");
64+
assertEquals(allCustomers.get(0).getLastName(), "Reynolds");
6565
}
6666

6767
@Test
6868
public void shouldDeleteCustomer() {
69-
CustomerDto customer = new CustomerDto("1", "David", "Roy");
69+
CustomerDto customer = new CustomerDto("1", "Terry", "Nguyen");
7070
List<CustomerDto> customers = new ArrayList<>();
7171
customers.add(customer);
7272

0 commit comments

Comments
 (0)