mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 20:59:29 +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:
@@ -32,7 +32,6 @@ import javax.sql.DataSource;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.joda.money.Money;
|
||||
|
||||
|
||||
/**
|
||||
* Domain Model pattern is a more complex solution for organizing domain logic than Transaction
|
||||
* Script and Table Module. It provides an object-oriented way of dealing with complicated logic.
|
||||
@@ -42,10 +41,10 @@ import org.joda.money.Money;
|
||||
* is that in Table Module a single class encapsulates all the domain logic for all records stored
|
||||
* in table when in Domain Model every single class represents only one record in underlying table.
|
||||
*
|
||||
* <p>In this example, we will use the Domain Model pattern to implement buying of products
|
||||
* by customers in a Shop. The main method will create a customer and a few products.
|
||||
* Customer will do a few purchases, try to buy product which are too expensive for him,
|
||||
* return product which he bought to return money.</p>
|
||||
* <p>In this example, we will use the Domain Model pattern to implement buying of products by
|
||||
* customers in a Shop. The main method will create a customer and a few products. Customer will do
|
||||
* a few purchases, try to buy product which are too expensive for him, return product which he
|
||||
* bought to return money.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@@ -80,11 +79,7 @@ public class App {
|
||||
var customerDao = new CustomerDaoImpl(dataSource);
|
||||
|
||||
var tom =
|
||||
Customer.builder()
|
||||
.name("Tom")
|
||||
.money(Money.of(USD, 30))
|
||||
.customerDao(customerDao)
|
||||
.build();
|
||||
Customer.builder().name("Tom").money(Money.of(USD, 30)).customerDao(customerDao).build();
|
||||
|
||||
tom.save();
|
||||
|
||||
|
||||
@@ -36,9 +36,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.joda.money.Money;
|
||||
|
||||
/**
|
||||
* This class organizes domain logic of customer.
|
||||
* A single instance of this class
|
||||
* contains both the data and behavior of a single customer.
|
||||
* This class organizes domain logic of customer. A single instance of this class contains both the
|
||||
* data and behavior of a single customer.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@@ -51,9 +50,7 @@ public class Customer {
|
||||
@NonNull private String name;
|
||||
@NonNull private Money money;
|
||||
|
||||
/**
|
||||
* Save customer or update if customer already exist.
|
||||
*/
|
||||
/** Save customer or update if customer already exist. */
|
||||
public void save() {
|
||||
try {
|
||||
Optional<Customer> customer = customerDao.findByName(name);
|
||||
@@ -117,9 +114,7 @@ public class Customer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print customer's purchases.
|
||||
*/
|
||||
/** Print customer's purchases. */
|
||||
public void showPurchases() {
|
||||
Optional<String> purchasesToShow =
|
||||
purchases.stream()
|
||||
@@ -133,9 +128,7 @@ public class Customer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print customer's money balance.
|
||||
*/
|
||||
/** Print customer's money balance. */
|
||||
public void showBalance() {
|
||||
LOGGER.info(name + " balance: " + money);
|
||||
}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.domainmodel;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* DAO interface for customer transactions.
|
||||
*/
|
||||
/** DAO interface for customer transactions. */
|
||||
public interface CustomerDao {
|
||||
|
||||
Optional<Customer> findByName(String name) throws SQLException;
|
||||
|
||||
@@ -32,9 +32,7 @@ import java.util.Optional;
|
||||
import javax.sql.DataSource;
|
||||
import org.joda.money.Money;
|
||||
|
||||
/**
|
||||
* Implementations for database operations of Customer.
|
||||
*/
|
||||
/** Implementations for database operations of Customer. */
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
@@ -40,9 +40,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.joda.money.Money;
|
||||
|
||||
/**
|
||||
* This class organizes domain logic of product.
|
||||
* A single instance of this class
|
||||
* contains both the data and behavior of a single product.
|
||||
* This class organizes domain logic of product. A single instance of this class contains both the
|
||||
* data and behavior of a single product.
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@@ -59,9 +58,7 @@ public class Product {
|
||||
@NonNull private Money price;
|
||||
@NonNull private LocalDate expirationDate;
|
||||
|
||||
/**
|
||||
* Save product or update if product already exist.
|
||||
*/
|
||||
/** Save product or update if product already exist. */
|
||||
public void save() {
|
||||
try {
|
||||
Optional<Product> product = productDao.findByName(name);
|
||||
@@ -75,16 +72,14 @@ public class Product {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate sale price of product with discount.
|
||||
*/
|
||||
/** Calculate sale price of product with discount. */
|
||||
public Money getSalePrice() {
|
||||
return price.minus(calculateDiscount());
|
||||
}
|
||||
|
||||
private Money calculateDiscount() {
|
||||
if (ChronoUnit.DAYS.between(LocalDate.now(), expirationDate)
|
||||
< DAYS_UNTIL_EXPIRATION_WHEN_DISCOUNT_ACTIVE) {
|
||||
< DAYS_UNTIL_EXPIRATION_WHEN_DISCOUNT_ACTIVE) {
|
||||
|
||||
return price.multipliedBy(DISCOUNT_RATE, RoundingMode.DOWN);
|
||||
}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.domainmodel;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* DAO interface for product transactions.
|
||||
*/
|
||||
/** DAO interface for product transactions. */
|
||||
public interface ProductDao {
|
||||
|
||||
Optional<Product> findByName(String name) throws SQLException;
|
||||
|
||||
@@ -33,9 +33,7 @@ import java.util.Optional;
|
||||
import javax.sql.DataSource;
|
||||
import org.joda.money.Money;
|
||||
|
||||
/**
|
||||
* Implementations for database transactions of Product.
|
||||
*/
|
||||
/** Implementations for database transactions of Product. */
|
||||
public class ProductDaoImpl implements ProductDao {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
Reference in New Issue
Block a user