Professional Documents
Culture Documents
LECTURE 12
DESIGNING OBJECTS WITH RESPONSIBILITIES
GRASP PATTERNS
1
Sample UP Artifact Relationships for Use-Case Realization
Domain Model
: System
Process Sale Operation: makeNewSale
: Cashier make
1. Customer NewSale() Post-conditions:
arrives ... - ...
2. Cashier system system
makes new enterItem
events operations
sale. (id, quantity)
conceptual 3. Cashier Operation: enterItem
classes in enters item
the identifier. endSale()
Post-conditions:
domain 4.... - A SalesLineItem instance
inspire the sli was created
names of makePayment -...
some (amount)
software
classes in Use Cases System Sequence Diagrams Contracts
the design in addition to the use
cases, requirements that
some ideas and inspiration for the post- must be satisfied by the
conditions derive from the use cases design of the software
makeNewSale()
create()
: Sale
enterItem
(itemID, quantity)
spec := getSpecification( itemID )
addLineItem( spec, quantity )
...
endSale()
... ...
2
Responsibilities
Deciding what methods belong where, and how the objects should interact, is
most important
The GRASP patterns are a learning aid in understanding essential object design
GRASP stands for General Responsibility Assignment Software Patterns
UML defines Responsibility as
◦ “a contract or obligation of a class”
7
Responsibilities and Methods
Doing responsibilities of an object include
◦ Doing something itself such as creating an object or doing a calculation
◦ Initiating action in other objects
◦ Controlling and coordinating activities in other objects
8
Responsibilities and Methods
Examples of Responsibilities
◦ A Sale is responsible for creating SalesLineItem instances (doing)
◦ A Sale is responsible for knowing its total (knowing)
9
Responsibilities and Methods
Example: Collaborating with other objects
◦ The class Sale might define one or more methods to know its total
(say a getTotal method). To fulfill that responsibility, the Sale may
collaborate with other objects e.g. sending a getSubTotal message to
each SalesLineItem object
10
Responsibilities and Interaction
Diagrams
Creation of interaction diagrams need fundamental principles (called
Patterns) for assigning responsibilities to objects
: Sale
makePayment(cashTendered)
create(cashTendered)
: Payment
12
GRASP Patterns
First five GRASP patterns
◦ Information Expert
◦ Creator
◦ High Cohesion
◦ Low coupling
◦ Controller
There are other patterns as well, but these address very basic, common
questions and fundamental design issues
13
Information Expert
Problem
◦ What is a general principle of assigning responsibilities to objects?
Solution
◦ Assign a responsibility to the information Expert – the class that has
the information necessary to fulfill the responsibility
14
Information Expert
Problem Explanation
◦ A design model may define hundreds of software classes and an
application may require hundreds of responsibilities to be fulfilled
◦ When the interactions between objects are defined, we make
choices about the assignment of responsibilities to software classes
◦ If responsibility assignment is done well
◦ Systems tend to be easier to understand, maintain and extend and there
is more opportunity to reuse components in future applications
15
Information Expert - example
In the NextGenPOS application, some class needs to know the grand total
of a sale
By Information Expert we should look for the class that has the
information needed to determine the total
16
Information Expert - Example cont…
Sale
date
time
1
Contains
1.. *
Product
Sales Specification
LineItem * Described-by
1
description
quantity price
itemID
17
Information Expert
Example
◦ What Information is needed to determine the grand total?
◦ It is necessary to know about all the SalesLineItem instances of a sale and the
sum of their subtotal
◦ Sale class contains this information, therefore suitable for this responsibility
i.e. getTotal
◦ It is an Information Expert for this work
18
Information Expert
A partial solution
t := getTotal() Sale
:Sale
date
t ime
20
Information Expert – Example cont…
Sale SalesLineItem
date quantity
time
New method getSubtotal()
getTotal()
21
Information Expert - Example cont…
For knowing and answering its subtotal a SalesLineItem needs to
know the product price
22
Information Expert Sale
date
time
t := getTotal() 1 *: st := getSubtotal() lineItems[i] :
: Sale getTotal()
SalesLineItem
1.1: p := SalesLineItem
Conclusion getPrice() quantity
To fulfill the responsibility of :Product getSubtotal()
knowing and answering Specification
◦ High Cohesion
24
Creator
Problem
◦ Who should be responsible for creating a new instance of some class?
25
Creator
Solution
◦ Assign class B the responsibility to create an instance of class A if one or more of the
following conditions is true
◦ B contains or compositely aggregates A objects.
◦ B records instances of A objects.
◦ B closely uses A objects.
◦ B has initializing data that will be passed to A when it is created
B is creator of A objects.
If more than one options, prefer a class B which compositely aggregates or contains class A
26
Creator
Sometimes a creator is found by looking for the class that has the initializing
◦ Example
total
◦ Since Sale knows the total, Sale is a candidate creator of the payment
27
Creator – Example cont…
In the POS application, who should be responsible for creating a
SalesLineItem instance?
By Creator,
◦ We should look for a class that aggregates, contains and so on,
SalesLineItem instances
28
Creator
Consider partial domain Model
Sale
date
time
1
Contains
1.. *
Product
Sales Specification
LineItem * Described-by 1
description
quantity price
itemID
29
Creator
Example
◦ Since Sale contains many SalesLineItem Objects
30
Creator
: Register : Sale
makeLineItem(quantity)
create(quantity) lineItems:
List<SalesLineItem>
Related Patterns
◦ Low coupling
◦ Factory
32
Who creates the Squares?
Coupling
Coupling is a measure of how strongly one element is connected to,
has knowledge of, or relies on other elements.
35
Coupling
A class with high or strong coupling relies on many other classes
◦ Harder to reuse because its use requires the additional presence of the
classes on which it is dependent.
36
Low Coupling
Problem
◦ How to support low dependency, low change impact and increased
reuse?
Solution
◦ Assign responsibilities so that coupling remains low
37
Low Coupling - Example
Consider the following partial diagram
38
Low Coupling-Example cont…
Register records a payment in real world domain
39
Low Coupling
2: addPayment(p)
:Sale
40
Low Coupling
An alternative solution
1.1. create()
:Payment
41
Low Coupling – Example cont…
Which design based on assignment of responsibilities supports low coupling?
In Diagram1, in which the Register creates the payment, adds coupling of payment
In Diagram2 Sale does the creation of payment and it does not increase coupling
42
Low Coupling - Benefits
Not affected by changes in other components
Convenient to reuse
43
Low Coupling
44
Cohesion
Cohesion is a measure of how strongly related and focused the
responsibilities of an element are.
A class with low cohesion does many unrelated things, or does too
much work.
45
Cohesion
Low Cohesion classes suffer from problems like;
◦ Hard to comprehend (understand)
◦ Hard to reuse
◦ Hard to maintain
46
High Cohesion
Problem
◦ How to keep complexity manageable?
Solution
◦ Assign a responsibility so that cohesion remains high
47
High Cohesion
Example (same example problem used in Low Coupling pattern can be
analyzed for High Cohesion)
◦ We have a need to create a Payment instance and associate it with Sale.
◦ Which class should be responsible for it?
◦ As discussed earlier, in real world domain Register records Payment
◦ Creator Pattern suggests Register as a candidate class for creating a
Payment instance
◦ Register instance could then send an addPayment message to the Sale
passing new Payment instance as a parameter
48
High Cohesion - Example
Register Creates Payment
2: addPayment(p)
:Sale
49
High Cohesion
Register Creates Payment
This single system event does not make Register class incohesive
But there exists many related system events (e.g. fifty system operations/events)
• Second design ( next example ) supports high cohesion and low coupling
50
High Cohesion
Sale Creates Payment
: Register : Sale
makePayment()
makePayment()
create()
: Payment
Since the second design supports both high cohesion and low coupling, it
is preferable
51
High Cohesion
52
High Cohesion
Functional Cohesion
◦ When the elements of a component (a class) “all work together to provide
some well-bounded behavior” [Grady Booch 94]
◦ Low Cohesion
◦ High Cohesion
◦ Moderate Cohesion
53
Functional Cohesion
Very Low Cohesion
◦ A class is solely responsible for many things in different functional areas
◦ Example
◦ Assume a class RDB-RPC-Interface
◦ That is completely responsible for interacting with Relational Databases and for Remote
Procedure Calls.
◦ These two are vastly different areas and each requires lots of supporting code
◦ Therefore, the responsibilities should be split into a family of classes related to RDB
access and a family related to RPC support
54
Functional Cohesion
Low Cohesion
◦ A class has sole responsibility for a complex task in one functional area
◦ Example
◦ Assume A class RDBInterface
◦ The methods of the class are all related, but there are lots of them, and there is a
tremendous amount of supporting code;
◦ There may be hundreds of methods
◦ The class should be split into a family of light-weight classes sharing the work to provide
RDB access
55
Functional Cohesion
High Cohesion
◦ A class has moderate (reasonable) responsibilities in one functional
area and collaborates with other classes to fulfill tasks
◦ Example
◦ Assume A class RDBInterface having partial responsibility for interacting with RDB
◦ It interacts with a dozen (may be) other classes related to RDB access in order to retrieve
and save objects
56
High Cohesion
Moderate Cohesion
◦ A class has lightweight and sole responsibilities in a few different areas
that are logically related to the class concept, but not to each other.
◦ Example
◦ Assume A class Company that is completely responsible for
◦ knowing its employees information and
◦ Knowing its financial information
◦ These two areas are not strongly related to each other although these two are logically
related to the concept of Company
57
High Cohesion - Benefits
Clarity and ease of comprehension (understanding) of the design
58
Controller Pattern
59
Controller
It is related with handling an input system event?
◦ An input system event is an event generated by an external actor
60
Controller
What is Controller?
61
Controller
Problem
◦ Who should be responsible for handling an input system event?
Solution
◦ Assign the responsibility for receiving or handling a system event
message to a class representing one of the following choices
◦ Class that represents the overall system, device or subsystem
◦ Class that represents a use case scenario within which the system event occurs,
often named as;
◦ <UseCaseName>Handler
◦ <UseCaseName>Coordinator
◦ <UseCaseName>Session
62
Controller - Example
In NextGenPOS there are different system operations
System
endSale()
enterItem()
makeNewSale()
makePayment()
...
presses button
: Cashier
actionPerformed( actionEvent )
Interface
Layer :SaleJFrame
System event message
enterItem(itemID, qty)
The class that represents a receiver or handler of all system events of a use
case scenario
65
Controller
enterItem(id, quantity)
:Register
enterItem(id, quantity)
:ProcessSaleHandler
66
Controller
Normally, a controller delegates the work to other objects
◦ It coordinates or controls the activity & does not do much work itself
Façade controller
◦ Represents the overall system, device or subsystem
◦ That provides the main point of service calls from the UI (User Interface) layer down
to other layers
67
Controller
Façade controllers are suitable when there are not too many system
events
68
Allocation of System Operations
System Register
endSale() ...
enterItem()
makeNewSale() endSale()
makePayment() enterItem()
makeNewSale()
makeNewReturn() makePayment()
enterReturnItem()
... makeNewReturn()
enterReturnItem()
...
ProcessSale HandleReturns
System Handler Handler
allocation of system
operations during design,
using several use case
controllers
69
Controller - Benefits
Increased potential for reuse and pluggable interfaces
◦ It ensures that Application logic is not handled in interface layer
70
Controller - Issues
Poorly designed a controller class will have low cohesion
Bloated Controller
◦ Unfocused and handling too many areas of responsibility
◦ Signs are
◦ A single controller class, receiving all system events
◦ Controller class itself performs many tasks without delegating work. This usually
involves violation of Information Expert and High Cohesion
◦ A controller has many attributes and maintains significant information about domain,
which should have been distributed to other objects, or duplicates information found
elsewhere.
71
Controller
Solutions to Bloated Controller
◦ Add more controllers – instead of façade, use use-case controllers
72
Controller
Related Patterns
◦ Command
◦ Façade
◦ Layers
◦ Separating domain logic from presentation layer
◦ Pure Fabrication
◦ Use case controller
73
Patterns for Presentation
Creational patterns Structural patterns Behavioral patterns
◦ Abstract Factory ◦ Adapter ◦ Chain of Responsibility
◦ Builder ◦ Bridge ◦ Command
◦ Factory Method ◦ Composite ◦ Interpreter
◦ Prototype ◦ Decorator ◦ Iterator
◦ Singleton ◦ Proxy ◦ Observer
◦ State
◦ Strategy
◦ Visitor
74