Professional Documents
Culture Documents
GRASP - General Responsibility Assignment Software Patterns Object Oriented Analysis and Design
GRASP - General Responsibility Assignment Software Patterns Object Oriented Analysis and Design
Aron Trauring T++ Technical Skills Training Program CUNY Institute for Software Design & Development (CISDD) New York Software Industry Association (NYSIA) December 17th, 2004
GRASP - OOAD
Mini Exercise 1 Who will create Square object in Monopoly? (Use DM Larman p.158)
GRASP - OOAD
GRASP - OOAD
Creator
Creator Problem: Who should be responsible for creating a new instance of some class? Solution: Assign class B the responsibility to create an instance of class A if one of these is true: 1. B contains or compositely aggregates A 2. B records A 3. B closely uses A
Aron Trauring Zoteca 3
GRASP - OOAD
4. B has the initializing data for A that will be passed to A when it is created (B is an expert w.r.t. A) If more than one applies, prefer a class B that meets criterion 1.
GRASP - OOAD
GRASP - OOAD
GRASP - OOAD
Creator Benets
Existing associations means created class is in any case visible to creator High cohesion Does not increase coupling
GRASP - OOAD
GRASP - OOAD
Information Expert
Information Expert Problem: What is a general principle of assigning responsibility to objects? Solution: Assign responsibility to the class that has the information necessary to fulll responsibility
GRASP - OOAD
10
GRASP - OOAD
11
GRASP - OOAD
getSubTotal == getPriceTotal
Aron Trauring Zoteca 12
GRASP - OOAD
getTotal == getSaleTotal
13
GRASP - OOAD
14
GRASP - OOAD
15
GRASP - OOAD
16
GRASP - OOAD
17
GRASP - OOAD
18
GRASP - OOAD
makePayment method invoked in Register A Payment has to be associated with the Sale Who creates the Payment instance?
19
GRASP - OOAD
Solution 1
20
GRASP - OOAD
Solution 1 Justication
Register records Payment Creator pattern addPayment method passes p instance of Payment as parameter Register coupled to Payment class
21
GRASP - OOAD
Solution 2
22
GRASP - OOAD
Coupling
Measure of how strongly one element is: 1. connected to 2. has knowledge of 3. relies on another element
23
GRASP - OOAD
24
GRASP - OOAD
Low Coupling
Low Coupling Problem: How to support low dependency, low change impact and increased reuse? Solution: Assign responsibility so coupling remains low. Use this principle to evaluate alternatives All other things being equal prefer the low coupling solution Note: Information Expert encourages Low Coupling
25
GRASP - OOAD
Types of Coupling
ClassX has an attribute (data member or instance variable) that refers to ClassY or ClassY instance Sale contains Orderline ClassX object calls on services of a ClassY object Product calls ProductCatalog.lookupPrice(itemID) ClassX has a method that references ClassY or ClassY instance In Orderline:
Aron Trauring Zoteca 26
GRASP - OOAD
def getPriceTotal(self): return (self.product.getPrice() * self.quantity) ClassX is a direct or indirect subclass of ClassY Class PizzaBot(Chef) ClassX is an interface, and ClassY implements that interface Lamp.Turnon
27
GRASP - OOAD
28
GRASP - OOAD
29
GRASP - OOAD
30
GRASP - OOAD
31
GRASP - OOAD
32
GRASP - OOAD
Controller
Controller Problem: What rst object beyond the UI layer receives and co-ordinates (controls ) a system operation Solution: Assign the responsibility to a class representing one of the following choices: 1. Facade Controller : represents the overall system, a root object, a device that the object is running within, or a major sub-system 2. Use Case or Session Controller : represents a use case scenario within which the system event occurs
Aron Trauring Zoteca 33
GRASP - OOAD
Session Controllers
Naming conventions: <UseCaseName>Handler or <UseCaseName>CoOrdinator or <UseCaseName>Session Use same controller class for all system operations in the use case scenario Session is a type of conversation between the actor and the SUD
34
GRASP - OOAD
35
GRASP - OOAD
Conceptual representation what is the the class that handles these operations?
Aron Trauring Zoteca 36
GRASP - OOAD
37
GRASP - OOAD
GRASP - OOAD
39
GRASP - OOAD
Controller Observations
Delegation pattern External input events may come from human actors or other systems Facade the face of the domain layer to the world Ex: Register Handler deals with a clear cut part of the issue Ex: ProcessSale Session conversation where information needs to be retained between steps
Aron Trauring Zoteca 40
GRASP - OOAD
41
GRASP - OOAD
Controller Facade
cover over the other layers of the application Abstraction of an overall physical unit Register, PizzaShop The entire software system POSSystem Abstraction of some other overall system or sub-system concept MonopolyGame Suitable for relatively small systems and/or system with limited number of system operations Suitable in message handling system when cant direct messages to alternative controllers Internet application servers
Aron Trauring Zoteca 42
GRASP - OOAD
43
GRASP - OOAD
Controller vs. UI
UI should not have responsibility for fullling system operations System operations handled in domain layer Controller is responsible for forwarding messages on
44
GRASP - OOAD
Controller Benets
Allows for easy change of UI and/or alternative UI and/or Batch mode Allows for reuse of domain layer code (UI is usually application specic) Helps ensure sequence of operation which may dier from UI input Can reason about system state UI does not preserve state
45
GRASP - OOAD
46
GRASP - OOAD
47
GRASP - OOAD
48
GRASP - OOAD
Cohesion Dened
measure of how strongly related and focused the responsibilities of an object are Goal: highly related responsibilities Goal: small number of responsibilities
49
GRASP - OOAD
50
GRASP - OOAD
High Cohesion
High Cohesion Problem: How to keep objects focused, understandable and manageable, and as a side eect support Low Coupling? Solution: Assign responsibility so cohesion remains high. Dosage: Use as an evaluation tool
51
GRASP - OOAD
Register creates Payment in real world, so by Creator should in software too Danger: Register might start taking on too many responsibilities One step in isolation might not be a problem but adding up will lead to low cohesion
Aron Trauring Zoteca 52
GRASP - OOAD
Delegating to Sale creates greater cohesion in Register Second example also has lower coupling, so therefore more desirable on that score
53
GRASP - OOAD
54
GRASP - OOAD
55
GRASP - OOAD
56