|
20 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | 21 | * THE SOFTWARE.
|
22 | 22 | */
|
| 23 | + |
23 | 24 | package com.iluwatar.roleobject;
|
24 | 25 |
|
| 26 | +import static com.iluwatar.roleobject.Role.Borrower; |
| 27 | +import static com.iluwatar.roleobject.Role.Investor; |
| 28 | + |
25 | 29 | import org.slf4j.Logger;
|
26 | 30 | import org.slf4j.LoggerFactory;
|
27 | 31 |
|
28 |
| -import static com.iluwatar.roleobject.Role.*; |
29 |
| - |
30 | 32 | /**
|
31 | 33 | * The Role Object pattern suggests to model context-specific views
|
32 | 34 | * of an object as separate role objects which are
|
|
39 | 41 | * investor, respectively. Both roles could as well be played by a single {@link Customer} object.
|
40 | 42 | * The common superclass for customer-specific roles is provided by {@link CustomerRole},
|
41 | 43 | * which also supports the {@link Customer} interface.
|
42 |
| - * <p> |
43 |
| - * The {@link CustomerRole} class is abstract and not meant to be instantiated. |
44 |
| - * Concrete subclasses of {@link CustomerRole}, for example {@link BorrowerRole} or {@link InvestorRole}, |
45 |
| - * define and implement the interface for specific roles. It is only |
| 44 | + * |
| 45 | + * <p>The {@link CustomerRole} class is abstract and not meant to be instantiated. |
| 46 | + * Concrete subclasses of {@link CustomerRole}, for example {@link BorrowerRole} |
| 47 | + * or {@link InvestorRole}, define and implement the interface for specific roles. It is only |
46 | 48 | * these subclasses which are instantiated at runtime.
|
47 |
| - * The {@link BorrowerRole} class defines the context-specific view of {@link Customer} objects as needed by the loan department. |
| 49 | + * The {@link BorrowerRole} class defines the context-specific view of {@link Customer} |
| 50 | + * objects as needed by the loan department. |
48 | 51 | * It defines additional operations to manage the customer’s
|
49 | 52 | * credits and securities. Similarly, the {@link InvestorRole} class adds operations specific
|
50 | 53 | * to the investment department’s view of customers.
|
51 |
| - * A client like the loan application may either work with objects of the {@link CustomerRole} class, using the interface class |
52 |
| - * {@link Customer}, or with objects of concrete {@link CustomerRole} subclasses. Suppose the loan application knows a particular |
53 |
| - * {@link Customer} instance through its {@link Customer} interface. The loan application may want to check whether the {@link Customer} |
54 |
| - * object plays the role of Borrower. |
55 |
| - * To this end it calls {@link Customer#hasRole(Role)} with a suitable role specification. For the purpose of |
56 |
| - * our example, let’s assume we can name roles with enum. |
57 |
| - * If the {@link Customer} object can play the role named “Borrower,” the loan application will ask it |
58 |
| - * to return a reference to the corresponding object. |
| 54 | + * A client like the loan application may either work with objects of the {@link CustomerRole} |
| 55 | + * class, using the interface class {@link Customer}, or with objects of concrete |
| 56 | + * {@link CustomerRole} subclasses. Suppose the loan application knows a particular |
| 57 | + * {@link Customer} instance through its {@link Customer} interface. The loan application |
| 58 | + * may want to check whether the {@link Customer} object plays the role of Borrower. |
| 59 | + * To this end it calls {@link Customer#hasRole(Role)} with a suitable role specification. For |
| 60 | + * the purpose of our example, let’s assume we can name roles with enum. |
| 61 | + * If the {@link Customer} object can play the role named “Borrower,” the loan application will |
| 62 | + * ask it to return a reference to the corresponding object. |
59 | 63 | * The loan application may now use this reference to call Borrower-specific operations.
|
60 | 64 | */
|
61 | 65 | public class ApplicationRoleObject {
|
62 | 66 |
|
63 |
| - private static final Logger logger = LoggerFactory.getLogger(Role.class); |
64 |
| - |
65 |
| - public static void main(String[] args) { |
66 |
| - Customer customer = Customer.newCustomer(Borrower, Investor); |
67 |
| - |
68 |
| - logger.info(" the new customer created : {}", customer); |
| 67 | + private static final Logger logger = LoggerFactory.getLogger(Role.class); |
69 | 68 |
|
70 |
| - boolean hasBorrowerRole = customer.hasRole(Borrower); |
71 |
| - logger.info(" customer has a borrowed role - {}", hasBorrowerRole); |
72 |
| - boolean hasInvestorRole = customer.hasRole(Investor); |
73 |
| - logger.info(" customer has an investor role - {}", hasInvestorRole); |
| 69 | + /** |
| 70 | + * Main entry point. |
| 71 | + * |
| 72 | + * @param args program arguments |
| 73 | + */ |
| 74 | + public static void main(String[] args) { |
| 75 | + Customer customer = Customer.newCustomer(Borrower, Investor); |
74 | 76 |
|
75 |
| - customer.getRole(Investor, InvestorRole.class) |
76 |
| - .ifPresent(inv -> { |
77 |
| - inv.setAmountToInvest(1000); |
78 |
| - inv.setName("Billy"); |
79 |
| - }); |
80 |
| - customer.getRole(Borrower, BorrowerRole.class) |
81 |
| - .ifPresent(inv -> inv.setName("Johny")); |
| 77 | + logger.info(" the new customer created : {}", customer); |
82 | 78 |
|
83 |
| - customer.getRole(Investor, InvestorRole.class) |
84 |
| - .map(InvestorRole::invest) |
85 |
| - .ifPresent(logger::info); |
| 79 | + boolean hasBorrowerRole = customer.hasRole(Borrower); |
| 80 | + logger.info(" customer has a borrowed role - {}", hasBorrowerRole); |
| 81 | + boolean hasInvestorRole = customer.hasRole(Investor); |
| 82 | + logger.info(" customer has an investor role - {}", hasInvestorRole); |
86 | 83 |
|
87 |
| - customer.getRole(Borrower, BorrowerRole.class) |
88 |
| - .map(BorrowerRole::borrow) |
89 |
| - .ifPresent(logger::info); |
90 |
| - } |
| 84 | + customer.getRole(Investor, InvestorRole.class) |
| 85 | + .ifPresent(inv -> { |
| 86 | + inv.setAmountToInvest(1000); |
| 87 | + inv.setName("Billy"); |
| 88 | + }); |
| 89 | + customer.getRole(Borrower, BorrowerRole.class) |
| 90 | + .ifPresent(inv -> inv.setName("Johny")); |
91 | 91 |
|
| 92 | + customer.getRole(Investor, InvestorRole.class) |
| 93 | + .map(InvestorRole::invest) |
| 94 | + .ifPresent(logger::info); |
92 | 95 |
|
| 96 | + customer.getRole(Borrower, BorrowerRole.class) |
| 97 | + .map(BorrowerRole::borrow) |
| 98 | + .ifPresent(logger::info); |
| 99 | + } |
93 | 100 | }
|
0 commit comments