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) {
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
/**
|
||||
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer.product;
|
||||
|
||||
/**
|
||||
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
||||
*/
|
||||
public final class Product {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Double price;
|
||||
private Double cost;
|
||||
private String supplier;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param id product id
|
||||
* @param name product name
|
||||
* @param price product price
|
||||
* @param cost product cost
|
||||
* @param supplier product supplier
|
||||
*/
|
||||
public Product(Long id, String name, Double price, Double cost, String supplier) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.cost = cost;
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public Product() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Product setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Product setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public Product setPrice(Double price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Product setCost(Double cost) {
|
||||
this.cost = cost;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSupplier() {
|
||||
return supplier;
|
||||
}
|
||||
|
||||
public Product setSupplier(String supplier) {
|
||||
this.supplier = supplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{"
|
||||
+ "id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ ", price=" + price
|
||||
+ ", cost=" + cost
|
||||
+ ", supplier='" + supplier + '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,287 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer.product;
|
||||
|
||||
/**
|
||||
* {@link ProductDto} is a data transfer object POJO.
|
||||
* Instead of sending individual information to
|
||||
* client We can send related information together in POJO.
|
||||
*
|
||||
* <p>Dto will not have any business logic in it.
|
||||
*/
|
||||
public enum ProductDto {
|
||||
;
|
||||
|
||||
/**
|
||||
* This is Request class which consist of Create or any other request DTO's
|
||||
* you might want to use in your API.
|
||||
*/
|
||||
public enum Request {
|
||||
;
|
||||
|
||||
/**
|
||||
* This is Create dto class for requesting create new product.
|
||||
*/
|
||||
public static final class Create implements Name, Price, Cost, Supplier {
|
||||
private String name;
|
||||
private Double price;
|
||||
private Double cost;
|
||||
private String supplier;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Create setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public Create setPrice(Double price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Create setCost(Double cost) {
|
||||
this.cost = cost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSupplier() {
|
||||
return supplier;
|
||||
}
|
||||
|
||||
public Create setSupplier(String supplier) {
|
||||
this.supplier = supplier;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is Response class which consist of any response DTO's
|
||||
* you might want to provide to your clients.
|
||||
*/
|
||||
public enum Response {
|
||||
;
|
||||
|
||||
/**
|
||||
* This is Public dto class for API response with the lowest data security.
|
||||
*/
|
||||
public static final class Public implements Id, Name, Price {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Double price;
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Public setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Public setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public Public setPrice(Double price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Public{"
|
||||
+ "id="
|
||||
+ id
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", price="
|
||||
+ price
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is Private dto class for API response with the highest data security.
|
||||
*/
|
||||
public static final class Private implements Id, Name, Price, Cost {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Double price;
|
||||
private Double cost;
|
||||
|
||||
@Override
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Private setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Private setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public Private setPrice(Double price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Private setCost(Double cost) {
|
||||
this.cost = cost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Private{"
|
||||
+
|
||||
"id="
|
||||
+ id
|
||||
+
|
||||
", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+
|
||||
", price="
|
||||
+ price
|
||||
+
|
||||
", cost="
|
||||
+ cost
|
||||
+
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Id in your DTO.
|
||||
*/
|
||||
private interface Id {
|
||||
/**
|
||||
* Unique identifier of the product.
|
||||
*
|
||||
* @return : id of the product.
|
||||
*/
|
||||
Long getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Name in your DTO.
|
||||
*/
|
||||
private interface Name {
|
||||
/**
|
||||
* The name of the product.
|
||||
*
|
||||
* @return : name of the product.
|
||||
*/
|
||||
String getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Price in your DTO.
|
||||
*/
|
||||
private interface Price {
|
||||
/**
|
||||
* The amount we sell a product for.
|
||||
* <b>This data is not confidential</b>
|
||||
*
|
||||
* @return : price of the product.
|
||||
*/
|
||||
Double getPrice();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Cost in your DTO.
|
||||
*/
|
||||
private interface Cost {
|
||||
/**
|
||||
* The amount that it costs us to purchase this product
|
||||
* For the amount we sell a product for, see the {@link Price Price} parameter.
|
||||
* <b>This data is confidential</b>
|
||||
*
|
||||
* @return : cost of the product.
|
||||
*/
|
||||
Double getCost();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Supplier in your DTO.
|
||||
*/
|
||||
private interface Supplier {
|
||||
/**
|
||||
* The name of supplier of the product or its manufacturer.
|
||||
* <b>This data is highly confidential</b>
|
||||
*
|
||||
* @return : supplier of the product.
|
||||
*/
|
||||
String getSupplier();
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer.product;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* The resource class which serves product information. This class act as server in the demo. Which
|
||||
* has all product details.
|
||||
*/
|
||||
public class ProductResource {
|
||||
private final List<Product> products;
|
||||
|
||||
/**
|
||||
* Initialise resource with existing products.
|
||||
*
|
||||
* @param products initialize resource with existing products. Act as database.
|
||||
*/
|
||||
public ProductResource(final List<Product> products) {
|
||||
this.products = products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all products.
|
||||
*
|
||||
* @return : all products in list but in the scheme of private dto.
|
||||
*/
|
||||
public List<ProductDto.Response.Private> getAllProductsForAdmin() {
|
||||
return products
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Private().setId(p.getId()).setName(p.getName())
|
||||
.setCost(p.getCost())
|
||||
.setPrice(p.getPrice()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all products.
|
||||
*
|
||||
* @return : all products in list but in the scheme of public dto.
|
||||
*/
|
||||
public List<ProductDto.Response.Public> getAllProductsForCustomer() {
|
||||
return products
|
||||
.stream()
|
||||
.map(p -> new ProductDto.Response.Public().setId(p.getId()).setName(p.getName())
|
||||
.setPrice(p.getPrice()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Save new product.
|
||||
*
|
||||
* @param createProductDto save new product to list.
|
||||
*/
|
||||
public void save(ProductDto.Request.Create createProductDto) {
|
||||
products.add(new Product()
|
||||
.setId((long) (products.size() + 1))
|
||||
.setName(createProductDto.getName())
|
||||
.setSupplier(createProductDto.getSupplier())
|
||||
.setPrice(createProductDto.getPrice())
|
||||
.setCost(createProductDto.getCost()));
|
||||
}
|
||||
|
||||
/**
|
||||
* List of all products in an entity representation.
|
||||
*
|
||||
* @return : all the products entity that stored in the products list
|
||||
*/
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
}
|
@ -21,13 +21,16 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.iluwatar.datatransfer.customer.CustomerDto;
|
||||
import com.iluwatar.datatransfer.customer.CustomerResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
Reference in New Issue
Block a user