|
| 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 | +} |
0 commit comments