Oose - Week 10 - Grasp Pattern I
Oose - Week 10 - Grasp Pattern I
Engineering
GRASP Pattern
Outline
Introduction
Creator
Information
Expert
GRASP Patterns
Introduction
Design
process
Whats GRASP pattern
Whats responsibility
Responsibilities and methods
Responsibilities and Interaction Diagrams
GRASP Patterns
Design Process
After
GRASP Patterns
What is Pattern?
It is common that when experts works on particular problems, they need not
reinvent the wheel i.e. they need not find a complete new solution that is
completely different from existing solutions. Rather they remember the
similar problem and it solution and reuse the core of it for solving new
problem. They think in terms of Problem Solution pairs. Abstracting
from such pairs and churning out the commonalities leads to patterns.
Each pattern is three-part rule which expresses a relation between a certain
GRASP Patterns
General Responsibility
Assignment Software Patterns
Craig
GRASP Patterns
GRASP
GRASP Patterns
The
Whats responsibility
Doing:
Doing something itself, such as creating an object or
doing a calculation
Initiating action in other objects
Controlling and coordinating activities in other objects.
Knowing:
Knowing about private encapsulated data
Knowing about related objects
Knowing about things it can derive or calculate
GRASP Patterns
create
:Payment
10
Responsibilities and
Interaction Diagrams
Interaction
GRASP Patterns
11
GRASP Patterns
9 GRASP patterns
Creator
Information Expert
Low Coupling
Controller
High Cohesion
Indirection
Polymorphism
Protected Variations
Pure Fabrication
GRASP Patterns
12
Creator
Problem: Who is responsible for creating new instances of some
class?
Solution: Assign class B the responsibility to create an instance of
class A if one or more of the following is true:
B aggregates A (simple aggregate; shared attributes)
B contains A (composition; non-shared attributes)
B records instances of A objects
B closely uses A objects
B has the initializing data that will be passed to A
when it is created (thus B is an Expert with respect to
creating A)
If more than one option applies, prefer a class B which aggregates or
contains class A.
GRASP Patterns
13
Creator
GRASP Patterns
15
Example of Creator
GRASP Patterns
16
Information Expert
Problem:
What is a general principle of assigning responsibilities
to objects?
Who should be responsible for knowing/doing ?
Domain model (domain expert, domain analysis) to design
Solution:
Assign a responsibility to the information expert
the class that has the information necessary to fulfill the
responsibility.
GRASP Patterns
17
Information Expert
Question
Do we look at the Domain Model or the Design Model to analyze the classes
that have the information needed?
Domain model illustrates conceptual classes, design model software classes
Answer
1. If there are relevant classes in the Design Model, look there first.
2. Otherwise, look in the Domain Model, and attempt to use (or expand) its
representations to inspire the creation of corresponding design classes.
Example on applying
Start by clearly stating the responsibility:
Who should be responsible for knowing the total of a sale?
Apply Information Expert pattern
Assume we are just starting design work and there is no or a
minimal Design Model, therefore
Product
date
description
price
itemID
GRASP Patterns
20
Example Contd
Sale
date
Product
SalesLineItem
description
price
itemID
quantity
21
Sale
Example Contd
date
getTotal
Product
SalesLineItem
description
price
itemID
quantity
getSubTotal
getPrice
GRASP Patterns
22
Example 2
Assume
Since
VideoStore
Example 2 contd