GRASP Patterns
GRASP Patterns
J.Serrat
102759 Software Design
http://www.cvc.uab.es/shared/teach/a21291/web/
Index
1
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
3 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
4 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
5 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
singleton
visitor
Principles
decorator
...
substitution
single
responsibility
favor
composition
over
inheritance
GRASP
patterns
open-closed
don't repeat
yourself
Encapsulation
Information hiding
Abstraction
Inheritance
Composition
6 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Learn [to design objects] from the successes of others, not from
their failures
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Whats ahead
Principles :
GRASP patterns :
Expert
Creator
High Cohesion
Low Coupling
Controller
Polymorphism
Pure
Fabrication
10 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP patterns
Pattern
A named pair (problem, solution) plus advice on when/how to
apply it and discussion of its trade-offs. In our case, solves an
objectoriented generic design problem.
GRASP: General Responsibility Assignment Software Patterns
Principles of responsibility assignment, expressed as patterns.
Responsibility of an object/class is doing or knowing
creating an object or doing a calculation
initiating an action or controlling activities in other objects
knowing about private, encapsulated data
reference or contain other objects
know how to get or calculate something
11 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP patterns
Make a class design
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
13 / 63
Pure fabrication
GRASP patterns
14 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Expert
Problem
What is a general principle of assigning responsibilities to classes ?
Solution
Assign responsibility to the information expert, the class that has
the information necessary to fulfil the responsibility.
15 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
16 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
17 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Is this right ?
18 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
19 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
20 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
21 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
22 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Creator
Problem
Who should be responsible for creating an instance of some class ?
Solution
Assign B the responsibility of create instances of A if
B aggregates or contains A objects
B closely uses A objects
B has the initializing data to be passed to the A constructor
23 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
24 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
25 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
class Register{
ArrayList<Sale> sales = new ArrayList<Sale>();
//...
public void addItemToSale(Sale sale, int itemID,
int quantity) {
ProductSpecification prodSpec
= productcatalog.searchproductSpecification(itemID);
sale.addLineItem(prodSpec, quatity);
}
}
class Sale {
ArrayList<SalesLineItem> salesLineItem
= new ArrayList<SalesLineItem>();
//...
public void addLineItem(ProductSpecification prodSpec,
int quantity) {
salesLineItem.add(new SalesLineItem(prodSpec, quantity);
}
}
26 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Benefits
Low coupling: anyway, the created class A has to know
(depend on) to the creator B
Exceptions
When creation is more complex, like instantiating objects of
different subclasses, or clone another object.
27 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
28 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
29 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
30 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
31 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
class Frequencies{
ArrayList<PrototipVehicle> prototips
= new ArrayList<PrototipVehicle>();
//...
public Vehicle getVehicle() {
double r = Math.random()*100.0;
double accumulatedFreq = 0.0;
iterator iter = prototips.Iterator();
while (iter.hasNext()) {
Prototip proto = (Prototip) iter.next();
double freq = proto.getFrequencia()
+ accumulatedFreq;
if ((r >= accumulatedFreq) && (r<freq)) {
return proto.getVehicle().clona(); // no clone()!
} else {
accumulatedFreq = freq;
}
}
}
32 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
35 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Types of coupling
36 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Problem
How to support low dependency, low change impact and increase
reuse ?
Solution
Assign responsibilities so that coupling remains low. Try to avoid
one class to have to know about many others.
37 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
38 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
39 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
40 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
41 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Discussion
extreme low coupling: few big, complex, non-cohesive classes
that do everything, plus many objects acting as data holders
it is not coupling per se the problem but coupling to unstable
classes
42 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
43 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Problem
How to keep classes focused, understandable and manageable ?
Solution Assign responsibilities so that cohesion remains high. Try
to avoid classes to do too much or too different things.
44 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
45 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
46 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
47 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Benefits
either the UI classes or the problem/software domain classes
can change without affecting the other side.
controller is a simple class that mediates between the UI and
problem domain classes, just forwards
event handling requests
output requests
48 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
49 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
50 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
51 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
52 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
53 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
Problems:
new shapes and class name changes require the modification
of this kind of code
normally it appears at several places
avoidable coupling between Client and shape subclasses
54 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
1
2
3
55 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
56 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
57 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
58 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
60 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
61 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
62 / 63
GRASP
Expert
Creator
Low coupling
High cohesion
Controller
Polymorphism
Pure fabrication
63 / 63