mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-17 02:59:23 +00:00
add pattern
This commit is contained in:
@@ -22,6 +22,11 @@
|
||||
*/
|
||||
package com.iluwatar.roleobject;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.iluwatar.roleobject.Role.*;
|
||||
|
||||
/**
|
||||
* The Role Object pattern suggests to model context-specific views
|
||||
* of an object as separate role objects which are
|
||||
@@ -34,12 +39,12 @@ package com.iluwatar.roleobject;
|
||||
* investor, respectively. Both roles could as well be played by a single Customer object.
|
||||
* The common superclass for customer-specific roles is provided by CustomerRole,
|
||||
* which also supports the Customer interface.
|
||||
* <p>
|
||||
* The CustomerRole class is abstract and not meant to be instantiated.
|
||||
* Concrete subclasses of CustomerRole, for example Borrower or Investor,
|
||||
* define and implement the interface for specific roles. It is only
|
||||
* these subclasses which are instantiated at runtime.
|
||||
* The Borrower class defines the context-specific view of
|
||||
* Customer objects as needed by the loan department.
|
||||
* The Borrower class defines the context-specific view of Customer objects as needed by the loan department.
|
||||
* It defines additional operations to manage the customer’s
|
||||
* credits and securities. Similarly, the Investor class adds operations specific
|
||||
* to the investment department’s view of customers.
|
||||
@@ -48,13 +53,40 @@ package com.iluwatar.roleobject;
|
||||
* Customer instance through its Customer interface. The loan application may want to check whether the Customer
|
||||
* object plays the role of Borrower.
|
||||
* To this end it calls hasRole() with a suitable role specification. For the purpose of
|
||||
* our example, let’s assume we can name roles with a simple string.
|
||||
* If the Customer object can play the role named
|
||||
* “Borrower,” the loan application will ask it to return a reference to the corresponding object.
|
||||
* our example, let’s assume we can name roles with enum.
|
||||
* If the 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.
|
||||
*
|
||||
*/
|
||||
public class ApplicationRoleObject {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Role.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Customer customer = Customer.newCustomer(Borrower, Investor);
|
||||
|
||||
logger.info(" the new customer created : {}", customer);
|
||||
|
||||
boolean hasBorrowerRole = customer.hasRole(Borrower);
|
||||
logger.info(" customer has a borrowed role - {}", hasBorrowerRole);
|
||||
boolean 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)
|
||||
.map(InvestorRole::invest)
|
||||
.ifPresent(logger::info);
|
||||
|
||||
customer.getRole(Borrower, BorrowerRole.class)
|
||||
.map(BorrowerRole::borrow)
|
||||
.ifPresent(logger::info);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user