6 Grasp
6 Grasp
6 Grasp
4. GRASP Patterns
Klaus Ostermann
21
Creator
} who has responsibility to create an object?
} By creator, assign class B responsibility of creating instance of
class A if
} B aggregates A objects
} B contains A objects
} B records instances of A objects
} B closely uses A objects
} B has the initializing data for creating A objects
} where there is a choice, prefer
} B aggregates or contains A objects
22
Creator : Example
Who is responsible for creating SalesLineItem objects?
Look for a class that aggregates or contains SalesLineItem objects.
Sale
date
time
Contain
s
1.. *
Product
Sales Specification
LineItem * Described-
by description
quantity price
UPC
23
Creator : Example
Creator pattern suggests Sale.
Collaboration diagram is
24
Creator
} Promotes low coupling by making instances of a class
responsible for creating objects they need to reference
} By creating the objects themselves, they avoid being
dependent on another class to create the object for them
25
Creator: Discussion
} Contraindications:
} creation may require significant complexity, such as
} using recycled instances for performance reasons
} conditionally creating an instance from one of a family of similar
classes based upon some external property value
} Sometimes desired to outsource object wiring (“dependency
injection”)
} Related patterns:
} Abstract Factory, Singleton, Dependency Injection
Solution:
Assign a responsibility so that coupling remains low.
28
Why High Coupling is undesirable
} Coupling is a measure of how strongly one element is
connected to, has knowledge of, or relies on other
elements.
} An element with low (or weak) coupling is not dependent
on too many other elements (classes, subsystems, …)
} "too many" is context-dependent
} A class with high (or strong) coupling relies on many
other classes.
} Changes in related classes force local changes.
} Such classes are harder to understand in isolation.
} They are harder to reuse because its use requires the
additional presence of the classes on which it is dependent.
30
Low Coupling
Two possibilities:
1. Register
2. Sale
32
Low Coupling: Discussion
} Low Coupling is a principle to keep in mind during all
design decisions
} It is an underlying goal to continually consider.
} It is an evaluative principle that a designer applies while
evaluating all design decisions.
} Low Coupling supports the design of classes that are
more independent
} reduces the impact of change.
} Can't be considered in isolation from other patterns such
as Expert and High Cohesion
} Needs to be included as one of several design principles
that influence a choice in assigning a responsibility.
36
High cohesion
} Classes are easier to maintain
} Easier to understand
} Often support low coupling
} Supports reuse because of fine grained responsibility
37
High Cohesion
Who has responsibility to create a payment?
1.Register
38
High Cohesion
Giving responsibility to Sale supports higher cohesion in Register, as well as
low coupling.
39
High Cohesion: Discussion
} Scenarios:
} Very Low Cohesion: A Class is solely responsible for many things in very
different functional areas
} Low Cohesion: A class has sole responsibility for a complex task in one
functional area.
} High Cohesion. A class has moderate responsibilities in one functional area
and collaborates with classes to fulfil tasks.
} Advantages:
} Classes are easier to maintain
} Easier to understand
} Often support low coupling
} Supports reuse because of fine grained responsibility
} Rule of thumb: a class with high cohesion has a relatively small
number of methods, with highly related functionality, and does not
do too much work.
40
Problem: High Cohesion and Viewpoints
43
Controller: Example
49
Polymorphism: Example
51
Nine GRASP patterns:
} Information Expert
} Creator
} Low Coupling
} Controller
} High Cohesion
} Polymorphism
} Indirection
} Pure Fabrication
} Protected Variations
Pure Fabrication
Problem:
Adding some responsibilities to domain objects
would violate high cohesion/low coupling/reuse
Solution:
Assign a highly cohesive set of responsibilities to an
artificial or convenience class that does not represent
a problem domain concept—something made up, to
support high cohesion, low coupling, and reuse.
53
Pure Fabrication: Example
} In the point of sale example support is needed to save Sale
instances in a relational database.
} By Expert, there is some justification to assign this
responsibility to Sale class.
} However, the task requires a relatively large number of
supporting database-oriented operations and the Sale class
becomes incohesive.
} The sale class has to be coupled to the relational database
increasing its coupling.
} Saving objects in a relational database is a very general task
for which many classes need support. Placing these
responsibilities in the Sale class suggests there is going to be
poor reuse or lots of duplication in other classes that do the
same thing.
54
Pure Fabrication : Example
} Solution: create a new class that is solely responsible for
saving objects in a persistent storage medium
} This class is a Pure Fabrication
56
Nine GRASP patterns:
} Information Expert
} Creator
} Low Coupling
} Controller
} High Cohesion
} Polymorphism
} Indirection
} Pure Fabrication
} Protected Variations
Indirection
Problem:
Where to assign a responsibility, to avoid direct coupling
between two (or more) things?
How to de-couple objects so that low coupling is supported
and reuse potential remains higher?
Solution:
Assign the responsibility to an intermediate object to mediate
between other components or services, so that they are not
directly coupled.
58
Indirection: Example
Solution:
Identify points of predicted variation or instability; assign
responsibilities to create a stable interface around them.