mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 00:58:24 +00:00
Merge data transfer object examples (#1635)
* #1617 merge dto pattern examples * #1617 update license headers Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
@@ -23,7 +23,13 @@
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
|
||||
import com.iluwatar.datatransfer.customer.CustomerDto;
|
||||
import com.iluwatar.datatransfer.customer.CustomerResource;
|
||||
import com.iluwatar.datatransfer.product.Product;
|
||||
import com.iluwatar.datatransfer.product.ProductDto;
|
||||
import com.iluwatar.datatransfer.product.ProductResource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -32,11 +38,17 @@ import org.slf4j.LoggerFactory;
|
||||
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
|
||||
* serve related information together to avoid multiple call for each piece of information.
|
||||
*
|
||||
* <p>In this example, ({@link App}) as as customer details consumer i.e. client to
|
||||
* request for customer details to server.
|
||||
* <p>In the first example, {@link App} is a customer details consumer i.e. client to
|
||||
* request for customer details to server. {@link CustomerResource} act as server to serve customer
|
||||
* information. {@link CustomerDto} is data transfer object to share customer information.
|
||||
*
|
||||
* <p>In the second example, {@link App} is a product details consumer i.e. client to
|
||||
* request for product details to server. {@link ProductResource} acts as server to serve
|
||||
* product information. {@link ProductDto} is data transfer object to share product information.
|
||||
*
|
||||
* <p>The pattern implementation is a bit different in each of the examples. The first can be
|
||||
* thought as a traditional example and the second is an enum based implementation.
|
||||
*
|
||||
* <p>CustomerResource ({@link CustomerResource}) act as server to serve customer information. And
|
||||
* The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@@ -48,6 +60,8 @@ public class App {
|
||||
* @param args program argument.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Example 1: Customer DTO
|
||||
var customerOne = new CustomerDto("1", "Kelly", "Brown");
|
||||
var customerTwo = new CustomerDto("2", "Alfonso", "Bass");
|
||||
var customers = new ArrayList<>(List.of(customerOne, customerTwo));
|
||||
@@ -72,6 +86,53 @@ public class App {
|
||||
customerResource.save(customerThree);
|
||||
allCustomers = customerResource.getAllCustomers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
// Example 2: Product DTO
|
||||
Product tv =
|
||||
new Product().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
|
||||
Product microwave =
|
||||
new Product()
|
||||
.setId(2L)
|
||||
.setName("microwave")
|
||||
.setSupplier("Delonghi")
|
||||
.setPrice(1000D)
|
||||
.setCost(1090D);
|
||||
Product refrigerator =
|
||||
new Product()
|
||||
.setId(3L)
|
||||
.setName("refrigerator")
|
||||
.setSupplier("Botsch")
|
||||
.setPrice(1000D)
|
||||
.setCost(1090D);
|
||||
Product airConditioner =
|
||||
new Product()
|
||||
.setId(4L)
|
||||
.setName("airConditioner")
|
||||
.setSupplier("LG")
|
||||
.setPrice(1000D)
|
||||
.setCost(1090D);
|
||||
List<Product> products =
|
||||
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
||||
ProductResource productResource = new ProductResource(products);
|
||||
|
||||
LOGGER.info(
|
||||
"####### List of products including sensitive data just for admins: \n {}",
|
||||
Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
|
||||
LOGGER.info(
|
||||
"####### List of products for customers: \n {}",
|
||||
Arrays.toString(productResource.getAllProductsForCustomer().toArray()));
|
||||
|
||||
LOGGER.info("####### Going to save Sony PS5 ...");
|
||||
ProductDto.Request.Create createProductRequestDto =
|
||||
new ProductDto.Request.Create()
|
||||
.setName("PS5")
|
||||
.setCost(1000D)
|
||||
.setPrice(1220D)
|
||||
.setSupplier("Sony");
|
||||
productResource.save(createProductRequestDto);
|
||||
LOGGER.info(
|
||||
"####### List of products after adding PS5: {}",
|
||||
Arrays.toString(productResource.getProducts().toArray()));
|
||||
}
|
||||
|
||||
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
|
||||
|
||||
Reference in New Issue
Block a user