mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 11:25:58 +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:
@@ -39,22 +39,22 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* customer-specific roles is provided by {@link CustomerRole}, which also supports the {@link
|
||||
* Customer} interface.
|
||||
*
|
||||
* <p>The {@link CustomerRole} class is abstract and not meant to be instantiated.
|
||||
* Concrete subclasses of {@link CustomerRole}, for example {@link BorrowerRole} or {@link
|
||||
* InvestorRole}, define and implement the interface for specific roles. It is only these subclasses
|
||||
* which are instantiated at runtime. The {@link BorrowerRole} class defines the context-specific
|
||||
* view of {@link Customer} objects as needed by the loan department. It defines additional
|
||||
* operations to manage the customer’s credits and securities. Similarly, the {@link InvestorRole}
|
||||
* class adds operations specific to the investment department’s view of customers. A client like
|
||||
* the loan application may either work with objects of the {@link CustomerRole} class, using the
|
||||
* interface class {@link Customer}, or with objects of concrete {@link CustomerRole} subclasses.
|
||||
* Suppose the loan application knows a particular {@link Customer} instance through its {@link
|
||||
* Customer} interface. The loan application may want to check whether the {@link Customer} object
|
||||
* plays the role of Borrower. To this end it calls {@link Customer#hasRole(Role)} with a suitable
|
||||
* role specification. For the purpose of our example, let’s assume we can name roles with enum. If
|
||||
* the {@link Customer} object can play the role named “Borrower,” the loan application will ask it
|
||||
* to return a reference to the corresponding object. The loan application may now use this
|
||||
* reference to call Borrower-specific operations.
|
||||
* <p>The {@link CustomerRole} class is abstract and not meant to be instantiated. Concrete
|
||||
* subclasses of {@link CustomerRole}, for example {@link BorrowerRole} or {@link InvestorRole},
|
||||
* define and implement the interface for specific roles. It is only these subclasses which are
|
||||
* instantiated at runtime. The {@link BorrowerRole} class defines the context-specific view of
|
||||
* {@link Customer} objects as needed by the loan department. It defines additional operations to
|
||||
* manage the customer’s credits and securities. Similarly, the {@link InvestorRole} class adds
|
||||
* operations specific to the investment department’s view of customers. A client like the loan
|
||||
* application may either work with objects of the {@link CustomerRole} class, using the interface
|
||||
* class {@link Customer}, or with objects of concrete {@link CustomerRole} subclasses. Suppose the
|
||||
* loan application knows a particular {@link Customer} instance through its {@link Customer}
|
||||
* interface. The loan application may want to check whether the {@link Customer} object plays the
|
||||
* role of Borrower. To this end it calls {@link Customer#hasRole(Role)} with a suitable role
|
||||
* specification. For the purpose of our example, let’s assume we can name roles with enum. If the
|
||||
* {@link Customer} object can play the role named “Borrower,” the loan application will ask it to
|
||||
* return a reference to the corresponding object. The loan application may now use this reference
|
||||
* to call Borrower-specific operations.
|
||||
*/
|
||||
@Slf4j
|
||||
public class ApplicationRoleObject {
|
||||
@@ -74,20 +74,23 @@ public class ApplicationRoleObject {
|
||||
var hasInvestorRole = customer.hasRole(INVESTOR);
|
||||
LOGGER.info("Customer has an investor role - {}", hasInvestorRole);
|
||||
|
||||
customer.getRole(INVESTOR, InvestorRole.class)
|
||||
.ifPresent(inv -> {
|
||||
inv.setAmountToInvest(1000);
|
||||
inv.setName("Billy");
|
||||
});
|
||||
customer.getRole(BORROWER, BorrowerRole.class)
|
||||
.ifPresent(inv -> inv.setName("Johny"));
|
||||
customer
|
||||
.getRole(INVESTOR, InvestorRole.class)
|
||||
.ifPresent(
|
||||
inv -> {
|
||||
inv.setAmountToInvest(1000);
|
||||
inv.setName("Billy");
|
||||
});
|
||||
customer.getRole(BORROWER, BorrowerRole.class).ifPresent(inv -> inv.setName("Johny"));
|
||||
|
||||
customer.getRole(INVESTOR, InvestorRole.class)
|
||||
customer
|
||||
.getRole(INVESTOR, InvestorRole.class)
|
||||
.map(InvestorRole::invest)
|
||||
.ifPresent(LOGGER::info);
|
||||
|
||||
customer.getRole(BORROWER, BorrowerRole.class)
|
||||
customer
|
||||
.getRole(BORROWER, BorrowerRole.class)
|
||||
.map(BorrowerRole::borrow)
|
||||
.ifPresent(LOGGER::info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.roleobject;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Borrower role.
|
||||
*/
|
||||
/** Borrower role. */
|
||||
@Getter
|
||||
@Setter
|
||||
public class BorrowerRole extends CustomerRole {
|
||||
@@ -39,5 +37,4 @@ public class BorrowerRole extends CustomerRole {
|
||||
public String borrow() {
|
||||
return String.format("Borrower %s wants to get some money.", name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.roleobject;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* The main abstraction to work with Customer.
|
||||
*/
|
||||
/** The main abstraction to work with Customer. */
|
||||
public abstract class Customer {
|
||||
|
||||
/**
|
||||
@@ -46,7 +44,6 @@ public abstract class Customer {
|
||||
* @param role to check
|
||||
* @return true if the role exists otherwise false
|
||||
*/
|
||||
|
||||
public abstract boolean hasRole(Role role);
|
||||
|
||||
/**
|
||||
@@ -60,13 +57,12 @@ public abstract class Customer {
|
||||
/**
|
||||
* Get specific instance associated with this role @see {@link Role}.
|
||||
*
|
||||
* @param role to get
|
||||
* @param role to get
|
||||
* @param expectedRole instance class expected to get
|
||||
* @return optional with value if the instance exists and corresponds expected class
|
||||
*/
|
||||
public abstract <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole);
|
||||
|
||||
|
||||
public static Customer newCustomer() {
|
||||
return new CustomerCore();
|
||||
}
|
||||
@@ -82,5 +78,4 @@ public abstract class Customer {
|
||||
Arrays.stream(role).forEach(customer::addRole);
|
||||
return customer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ public class CustomerCore extends Customer {
|
||||
|
||||
@Override
|
||||
public boolean addRole(Role role) {
|
||||
return role
|
||||
.instance()
|
||||
.map(inst -> {
|
||||
roles.put(role, inst);
|
||||
return true;
|
||||
})
|
||||
return role.instance()
|
||||
.map(
|
||||
inst -> {
|
||||
roles.put(role, inst);
|
||||
return true;
|
||||
})
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
@@ -66,8 +66,7 @@ public class CustomerCore extends Customer {
|
||||
|
||||
@Override
|
||||
public <T extends Customer> Optional<T> getRole(Role role, Class<T> expectedRole) {
|
||||
return Optional
|
||||
.ofNullable(roles.get(role))
|
||||
return Optional.ofNullable(roles.get(role))
|
||||
.filter(expectedRole::isInstance)
|
||||
.map(expectedRole::cast);
|
||||
}
|
||||
|
||||
@@ -24,8 +24,5 @@
|
||||
*/
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
/**
|
||||
* Key abstraction for segregated roles.
|
||||
*/
|
||||
public abstract class CustomerRole extends CustomerCore {
|
||||
}
|
||||
/** Key abstraction for segregated roles. */
|
||||
public abstract class CustomerRole extends CustomerCore {}
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.roleobject;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Investor role.
|
||||
*/
|
||||
/** Investor role. */
|
||||
@Getter
|
||||
@Setter
|
||||
public class InvestorRole extends CustomerRole {
|
||||
|
||||
@@ -29,12 +29,10 @@ import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Possible roles.
|
||||
*/
|
||||
/** Possible roles. */
|
||||
public enum Role {
|
||||
|
||||
BORROWER(BorrowerRole.class), INVESTOR(InvestorRole.class);
|
||||
BORROWER(BorrowerRole.class),
|
||||
INVESTOR(InvestorRole.class);
|
||||
|
||||
private final Class<? extends CustomerRole> typeCst;
|
||||
|
||||
@@ -44,18 +42,18 @@ public enum Role {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
|
||||
/**
|
||||
* Get instance.
|
||||
*/
|
||||
/** Get instance. */
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends CustomerRole> Optional<T> instance() {
|
||||
var typeCst = this.typeCst;
|
||||
try {
|
||||
return (Optional<T>) Optional.of(typeCst.getDeclaredConstructor().newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
} catch (InstantiationException
|
||||
| IllegalAccessException
|
||||
| NoSuchMethodException
|
||||
| InvocationTargetException e) {
|
||||
logger.error("error creating an object", e);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user