GRASP Patterns
GRASP Patterns
GRASP Patterns
Dave Levitt
CS 2000: Systems Analysis and
Design
Agenda
• Discuss E3
• Design Concepts
• GRASP Patterns
• Lab
Week 12 2
Design Concepts
• Review of what we’ve done so far:
– We’ve captured stakeholders requirements.
• How? What RUP phase(s)? So now we know all the requirements. Right?
Week 12 3
Design Concepts (cont’d)
• Design is a very creative activity:
– Think about clothing or room designers.
• What image(s) come to mind? What ‘rules’ do you think they follow? Is it art?
Science? Both? Neither?
Week 12 4
Design Concepts (cont’d)
• Design is all about tradeoffs:
– For a clothing designer, what might some tradeoff’s be?
Week 12 5
Design Concepts (cont’d)
• Think of a dressmakers pattern. How might you describe it?
Week 12 6
Design Concepts (cont’d)
• Patterns:
– Are not invented. They are harvested from existing solutions.
– Sometimes conflict with each other. For example: you apply a patterns to
solve one problem, but by doing so, you may introduce others.
– This is called a contradiction, or side-effect.
– These are the tradeoffs designers have to deal with!
Week 12 7
GRASP Patterns
• Larman introduces a set of basic patterns that he calls GRASP:
General Responsibility Assignment Software Pattern
Week 12 8
GRASP - Information Expert
• Problem: A system will have hundreds of classes. How do I begin
to assign responsibilities to them?
Week 12 9
GRASP - Information Expert (cont’d)
• Discussion Points:
– Throughout the process of assigning a responsibility, you may discover
many lower-level responsibilities. Example: Profit & Loss Statement.
What should you do then?
• Contradictions:
– Sometimes application of the Expert Pattern is undesirable. For example:
Who should save the sale to a database?
• Stay tuned….. We’ll discuss problems like this in Design Models?
Week 12 10
GRASP - Information Expert (cont’d)
contains
1..*
ProductSpecification
SalesLineIte m Describ ed_b y (f rom Classes )
(f rom Classes)
des cription
quantity price
1..* 1
ite mID
Week 12 11
GRASP - Information Expert (cont’d)
POS - Partial Domain Model
Who has the responsibility to calculate the total of a sale?
Sale
(f rom Classes)
Give this class the
date
responsibility to get the sales
time
total
1
contains
1..*
SalesLineItem ProductSpecification
Describ ed_b y (f rom Clas ses )
(f rom Classes)
quantity de scriptio n
1..* pri ce
1
itemID
Week 12 12
GRASP - Information Expert (cont’d)
: Sale : SalesLineItem :
: <Actor Na me>
Produ ctSp ecification
getTotal ( )
getSubtotal( )
getPrice()
Week 12 13
GRASP – Creator Pattern
• Problem: Who creates new instances of some class?
makeLineItem( ) create()
Week 12 15
GRASP – Low Coupling
• Concept – Coupling:
– Coupling refers to dementedness, or connectedness.
– Our goal is to design for low coupling, so that changes in one element
(sub-system, system, class, etc.) will limit changes to other elements.
Week 12 16
GRASP – Low Coupling (cont’d)
• Problem: How do you support low dependency, low change
impact, and increased reuse.
Week 12 17
GRASP – Low Coupling (cont’d)
• Discussion points:
– Is it OK to couple your design to let’s say a particular database?
Week 12 18
GRASP – Low Coupling (con’t)
Add ing a payment to a sale - version 1
makePaym ent( )
create()
a ddPaym ent( )
Add a payment to a sal e - vers ion 1. Note R egis ter's respons ibil itie s
Week 12 19
GRASP – Low Coupling (con’t)
Adding a payment to a sale - version 2
makePayment( )
makePayment( )
create()
Week 12 20
GRASP – High Cohesion
• Concept – Cohesion:
– Cohesion is a measure of “relatedness”.
– Low Cohesion says elements are not strongly related to one another. Ex:
• System level: ATM with a use case (function) called “Teller Reports”.
• Class level: A Student class with a method called “getDrivingRecord().
• Method level: Methods with the word “And” or “Or” in them.
• Also applies to subsystem (package) level, component level, etc.
Week 12 21
GRASP: High Cohesion (cont’d)
• Problem: How do you keep complexity manageable?
• Contradictions:
– High Cohesion and low coupling are competing forces. More later.
Week 12 22
GRASP – High Cohesion (con’t)
• Additional:
– Larman describes degrees of cohesion, from very low to high. For now, just
consider that in general, classes with a small number of functionally related
methods is more desirables than bloated classes with disconnected methods.
• Look at the sequence diagrams for adding a payment to a sale, but this time
from the perspective of cohesion. Which version supports high cohesion, and
why?
Week 12 23
GRASP - Controller
• Problem: Who handles events from external actors, e.g. startup(),
playSongs(), etc?
Week 12 24
GRASP – Controller (cont’d)
• Additional (cont’d):
– Watch for bloated controllers. This could be a sign of what kind of
failure?
Week 12 25
Assigning Responsibilities –
Other Sources
• CRC Cards:
– Another popular technique to assigning responsibilities to classes is to use
CRC cards. CRC = Class: Responsibility: Collaboration.
– Introduced by Kent Beck and Ward Cunnigham.
– Popularized by Rebecca Wirfs-Brock: Designing Object Oriented
Software
• Design Heuristics by Authur Riel. CS 2100 – Stay tuned!
• Web sites: Martin Fowler, Peter Coad.
Week 12 26