The Role Object Pattern Source Pattern Languages of
The Role Object Pattern Source: Pattern Languages of Programming (PLo. P) ’ 97 Conference Authors: Dirk Bäumer, Dirk Riehle, Wolf Siberski, Martina Wulf Student:Hsuan-Chang Ku Advisor: Ku-Yaw Chang H. C Ku, H 704, CSIE DYU
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 2
Motivation (1/10) n n An object-oriented system based on a set of key abstraction Suppose develop software support for bank’s investment department n n n One of the key abstraction is the concept of Customer Design model will include a Customer Class interface provides operations to manage properties n 12/2/2020 Customer’s name, address, savings and deposit account H. C Ku, H 704, CSIE DYU 3
Motivation (2/10) n The bank’s also needs software support for bank’s loan department n n It’s inadequate to deal with a customer acting as borrower Further implementation state and operation to manage n Customer’s loan accounts, credits, and securities 12/2/2020 H. C Ku, H 704, CSIE DYU 4
Motivation (3/10) Investment Dept. Item 1 Name Item 2 Address Item 3 Item 4 12/2/2020 Savings account Deposit account Loan Dept. Loan account Other Dept. Credit Securities H. C Ku, H 704, CSIE DYU 5
Motivation (4/10) n An object-oriented system based on a set of key abstraction n Each one is modeled by a corresponding class in terms of abstract state and behavior Works fine for the small application Once scale up the system into an integrated suite of applications n Deal with different clients that needs contextspecific views on key abstraction 12/2/2020 H. C Ku, H 704, CSIE DYU 6
Motivation (5/10) n Integrating several context-specific views in the same class n n Most likely lead to key abstractions with bloated interface Such interface are difficult to understand hard to maintain 12/2/2020 H. C Ku, H 704, CSIE DYU 7
Motivation (6/10) n Separate role objects dynamically attached to and removed from the core object Object 12/2/2020 Role … Object Played by Play Role … Role H. C Ku, H 704, CSIE DYU 8
Motivation (7/10) n Key abstraction : Customer n n n Abstract superclass Pure interface which doesn’t define any implementation state Customer class n n Specify operations to handle address and account Define a minimal protocol for managing roles 12/2/2020 H. C Ku, H 704, CSIE DYU 9
Motivation (8/10) n Customer. Core n n Implements the Customer interface Customer. Role n n n Abstract Not meant to be instantiated Ex : Borrower : n 12/2/2020 define the context-specific view when needed by loan dept. H. C Ku, H 704, CSIE DYU 10
Motivation (9/10) 12/2/2020 H. C Ku, H 704, CSIE DYU 11
Motivation (10/10) 12/2/2020 H. C Ku, H 704, CSIE DYU 12
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 15
Applicability (1/2) ─ If you want to n Handle a key abstraction n n In different contexts and don’t want to put the resulting context-specific interface into the same class interface Handle the available role dynamically n Attached & removed on demand at runtime rather than fix them statically at compile-time 12/2/2020 H. C Ku, H 704, CSIE DYU 16
Applicability (2/2) ─ If you want to n n Treat the extensions transparently & need to preserve the logical object identity of the resulting object conglomerate Keep role/client pairs independent from each other n Change a role don’t affect clients that aren’t interested in that role 12/2/2020 H. C Ku, H 704, CSIE DYU 17
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 18
Structure 12/2/2020 H. C Ku, H 704, CSIE DYU 19
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 20
Participants (1/2) n Component (Customer) n n n Models a particular key abstraction by define it’s interface Protocol : add, remove, test, query Component. Core (Customer. Core) n n n Implement interface include the role management protocol Create Concrete. Role instances Manage its role object 12/2/2020 H. C Ku, H 704, CSIE DYU 21
Participants (2/2) n Component. Role (Customer. Role) n n n Store a reference to the decorated Component. Core Implement the Component interface by forwarding requests to its core attribute Concrete. Role (Investor, Borrower) n n Model & implement a context-specific extension Instantiated with a Component. Core as argument 12/2/2020 H. C Ku, H 704, CSIE DYU 22
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 23
Collaborations n Core and role objects n n Component. Role forwards requests to its Component. Core object Concrete. Core instantiates and manages Concrete. Roles 12/2/2020 H. C Ku, H 704, CSIE DYU 24
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 25
Consequences (1/2) n Advantages and consequences n n The key abstraction can be defined concisely Roles can be evolved easily and independently of each other Roles objects can be added and removed dynamically Applications get better decoupled 12/2/2020 H. C Ku, H 704, CSIE DYU 26
Consequences (2/2) n Disadvantages and liabilities n n Clients are likely to get more complex Maintaining constraints between roles becomes difficult Constraints on roles can’t be enforced by the type system Maintaining objects identity gets more complex 12/2/2020 H. C Ku, H 704, CSIE DYU 27
Outline n n n n Motivation Intent Applicability Structure Participants Collaborations Consequences Implementation 12/2/2020 H. C Ku, H 704, CSIE DYU 28
Implementation (1/4) ─ Two critical issues n Transparently extending key abstractions with roles n n Decorator pattern Dynamically managing these roles n Product Trader pattern 12/2/2020 H. C Ku, H 704, CSIE DYU 29
Implementation (2/4) ─ “Hiding the role object creation process” n n Key issue: How a Concrete. Role instance is actually created and attached to the core object Role creation process should be initiated by Component. Core Avoiding role objects may exist of their own Preventing clients from knowing how to instantiate role objects 12/2/2020 H. C Ku, H 704, CSIE DYU 30
Implementation (3/4) ─ “Maintaining constraints among roles by recursively applying the Role Object pattern” 12/2/2020 H. C Ku, H 704, CSIE DYU 31
Implementation (4/4) ─ “Maintaining constraints among roles by recursively applying the Role Object pattern” n A chain of role and core objects 12/2/2020 H. C Ku, H 704, CSIE DYU 32
Thanks! 12/2/2020 H. C Ku, H 704, CSIE DYU 33
- Slides: 31