grasp pattern
grasp pattern
Design Patterns
Maryam Mir
[email protected]
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”
• Responsibilities are related to the obligations of an object in terms of its
behavior
• Responsibilities are of two types offers a common way to speak about
• Doing abstract concepts
• Knowing
3
Responsibilities and Methods
4
Responsibilities and Methods
• Examples of Responsibilities
• A Sale is responsible for creating SalesLineItem instances (doing)
• A Sale is responsible for knowing its total (knowing)
5
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
6
Responsibilities and Interaction Diagrams
• Creation of interaction diagrams need fundamental principles
(called Patterns) for assigning responsibilities to objects
: Sale
makePayment(cashTendered)
create(cashTendered)
: Payment
8
GRASP Patterns
•First five GRASP patterns
• Information Expert
• Creator
• High Cohesion
• Low coupling
• Controller
9
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
10
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
11
Information Expert - example
Sale
date
time
1
Contains
1.. *
Product
Sales Specification
LineItem * Described-by
1
description
quantity price
itemID
13
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
14
Information Expert – Example cont…
• What information is needed to determine the SlaesLineItem
subtotal?
15
Information Expert - Example cont…
16
Information Expert - Benefits
18
Coupling
19
Low Coupling
• Problem
• How to support low dependency, low change impact and
increased reuse?
• Solution
• Assign responsibilities so that coupling remains low
20
Low Coupling - Example
• Consider the following partial diagram
21
Low Coupling-Example cont…
• Register records a payment in real world domain
22
Low Coupling
2: addPayment(p)
:Sale
23
Low Coupling
• An alternative solution
1.1. create()
:Payment
24
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
25
Low Coupling - Benefits
• Not affected by changes in other components
• Convenient to reuse
26
Low Coupling
27
Cohesion
• Cohesion is a measure of how strongly related and focused
the responsibilities of an element are.
• An element with highly related responsibilities and which does
not do a tremendous amount of work, has high cohesion.
• A class with low cohesion does many unrelated things, or
does too much work.
• Such classes are undesirable; they suffer from many problems
28
Cohesion
• Low Cohesion classes suffer from problems like;
• Hard to comprehend (understand)
• Hard to reuse
• Hard to maintain
29
High Cohesion
• Problem
• How to keep complexity manageable?
• Solution
• Assign a responsibility so that cohesion remains high
30
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
31
High Cohesion - Example
• Register Creates Payment
2: addPayment(p)
:Sale
32
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
33
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
34
High Cohesion
35
High Cohesion
• Functional Cohesion
• When the elements of a component (a class) “all work together to
provide some well-bounded behavior” [Grady Booch 94]
36