mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 20:59:07 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -38,17 +38,16 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* 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 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 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>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.
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -88,28 +87,32 @@ public class App {
|
||||
|
||||
// Example 2: Product DTO
|
||||
|
||||
Product tv = Product.builder().id(1L).name("TV").supplier("Sony").price(1000D).cost(1090D).build();
|
||||
Product tv =
|
||||
Product.builder().id(1L).name("TV").supplier("Sony").price(1000D).cost(1090D).build();
|
||||
Product microwave =
|
||||
Product.builder()
|
||||
.id(2L)
|
||||
.name("microwave")
|
||||
.supplier("Delonghi")
|
||||
.price(1000D)
|
||||
.cost(1090D).build();
|
||||
.cost(1090D)
|
||||
.build();
|
||||
Product refrigerator =
|
||||
Product.builder()
|
||||
.id(3L)
|
||||
.name("refrigerator")
|
||||
.supplier("Botsch")
|
||||
.price(1000D)
|
||||
.cost(1090D).build();
|
||||
.cost(1090D)
|
||||
.build();
|
||||
Product airConditioner =
|
||||
Product.builder()
|
||||
.id(4L)
|
||||
.name("airConditioner")
|
||||
.supplier("LG")
|
||||
.price(1000D)
|
||||
.cost(1090D).build();
|
||||
.cost(1090D)
|
||||
.build();
|
||||
List<Product> products =
|
||||
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
||||
ProductResource productResource = new ProductResource(products);
|
||||
|
||||
@@ -24,9 +24,6 @@
|
||||
*/
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to
|
||||
* client We can send related information together in POJO.
|
||||
|
||||
-2
@@ -25,8 +25,6 @@
|
||||
package com.iluwatar.datatransfer.customer;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* The resource class which serves customer information. This class act as server in the demo. Which
|
||||
|
||||
@@ -29,9 +29,7 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
||||
*/
|
||||
/** {@link Product} is a entity class for product entity. This class act as entity in the demo. */
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@@ -46,11 +44,18 @@ public final class Product {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{"
|
||||
+ "id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ ", price=" + price
|
||||
+ ", cost=" + cost
|
||||
+ ", supplier='" + supplier + '\''
|
||||
+ '}';
|
||||
+ "id="
|
||||
+ id
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", price="
|
||||
+ price
|
||||
+ ", cost="
|
||||
+ cost
|
||||
+ ", supplier='"
|
||||
+ supplier
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
+24
-56
@@ -25,8 +25,7 @@
|
||||
package com.iluwatar.datatransfer.product;
|
||||
|
||||
/**
|
||||
* {@link ProductDto} is a data transfer object POJO.
|
||||
* Instead of sending individual information to
|
||||
* {@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.
|
||||
@@ -35,15 +34,13 @@ 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.
|
||||
* 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.
|
||||
*/
|
||||
/** 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;
|
||||
@@ -93,15 +90,13 @@ public enum ProductDto {
|
||||
}
|
||||
|
||||
/**
|
||||
* This is Response class which consist of any response DTO's
|
||||
* you might want to provide to your clients.
|
||||
* 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.
|
||||
*/
|
||||
/** 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;
|
||||
@@ -139,21 +134,11 @@ public enum ProductDto {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Public{"
|
||||
+ "id="
|
||||
+ id
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", price="
|
||||
+ price
|
||||
+ '}';
|
||||
return "Public{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + '}';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is Private dto class for API response with the highest data security.
|
||||
*/
|
||||
/** 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;
|
||||
@@ -203,28 +188,21 @@ public enum ProductDto {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Private{"
|
||||
+
|
||||
"id="
|
||||
+ "id="
|
||||
+ id
|
||||
+
|
||||
", name='"
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+
|
||||
", price="
|
||||
+ ", price="
|
||||
+ price
|
||||
+
|
||||
", cost="
|
||||
+ ", cost="
|
||||
+ cost
|
||||
+
|
||||
'}';
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Id in your DTO.
|
||||
*/
|
||||
/** Use this interface whenever you want to provide the product Id in your DTO. */
|
||||
private interface Id {
|
||||
/**
|
||||
* Unique identifier of the product.
|
||||
@@ -234,9 +212,7 @@ public enum ProductDto {
|
||||
Long getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Name in your DTO.
|
||||
*/
|
||||
/** Use this interface whenever you want to provide the product Name in your DTO. */
|
||||
private interface Name {
|
||||
/**
|
||||
* The name of the product.
|
||||
@@ -246,40 +222,32 @@ public enum ProductDto {
|
||||
String getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this interface whenever you want to provide the product Price in your DTO.
|
||||
*/
|
||||
/** 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>
|
||||
* 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.
|
||||
*/
|
||||
/** 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>
|
||||
* 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.
|
||||
*/
|
||||
/** 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>
|
||||
* The name of supplier of the product or its manufacturer. <b>This data is highly
|
||||
* confidential</b>
|
||||
*
|
||||
* @return : supplier of the product.
|
||||
*/
|
||||
|
||||
+24
-17
@@ -37,11 +37,14 @@ public record ProductResource(List<Product> 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()))
|
||||
return products.stream()
|
||||
.map(
|
||||
p ->
|
||||
new ProductDto.Response.Private()
|
||||
.setId(p.getId())
|
||||
.setName(p.getName())
|
||||
.setCost(p.getCost())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -51,10 +54,13 @@ public record ProductResource(List<Product> 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()))
|
||||
return products.stream()
|
||||
.map(
|
||||
p ->
|
||||
new ProductDto.Response.Public()
|
||||
.setId(p.getId())
|
||||
.setName(p.getName())
|
||||
.setPrice(p.getPrice()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -64,12 +70,13 @@ public record ProductResource(List<Product> products) {
|
||||
* @param createProductDto save new product to list.
|
||||
*/
|
||||
public void save(ProductDto.Request.Create createProductDto) {
|
||||
products.add(Product.builder()
|
||||
.id((long) (products.size() + 1))
|
||||
.name(createProductDto.getName())
|
||||
.supplier(createProductDto.getSupplier())
|
||||
.price(createProductDto.getPrice())
|
||||
.cost(createProductDto.getCost())
|
||||
.build());
|
||||
products.add(
|
||||
Product.builder()
|
||||
.id((long) (products.size() + 1))
|
||||
.name(createProductDto.getName())
|
||||
.supplier(createProductDto.getSupplier())
|
||||
.price(createProductDto.getPrice())
|
||||
.cost(createProductDto.getCost())
|
||||
.build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user