Professional Documents
Culture Documents
Example Consider the following partial class diagram from a point-of-sale terminal appli-
cation:
m Payment
Assume we have a need to create a Payment instance and associate it with the
Sale. What class should be responsiRG for-<hi&? Since a POST "records" a Pa?-
ment in the real-world domain, the Creator pattern suggests POST as a candi-
date for creating the Payment. The Post instance could then send an
addpayment message to the Sale, passing along the new Payment as a parame-
ter. A possible partial collaboration diagram reflecting this is shown in Figure
18.9.
---&
p : Pavrnent
Example The same example problem used in the Low Coupling pattern can be analyzed
for High Cohesion.
-- -
kssu<6-we h a v e - e e e d to create aa_(c.ash)
Parment=iwtance and associate it_
wxh the Sale. @hat -- claT3.sh6;ld be fesponsibie
- - for this? since P O S T % C O ~ ~ Sa
k tthe real-world domain, t_he(2~_eatg-$&m suggests POST as a can-
~ a ~ & e in
didate for creating the Payment. me - - could then send an addpay-
-_Post instance
ment message to the Sale, passing along the new Payment as a parameter, as
shown in Figure 18.11.
.. _. 7.
;
1: create()---
,-. .,-:,:- .- ..- ..
p : Pavment
, , ,.
.".-.
L: y:
"
class responsible for doing s&e-or most of the work related t~-in;re and more
- -
system operations, it-will become
- --
increasingly burdened with tasks and becom
- -- - ---
incohes&,ve_.-
-
.- .-
In practice, the level of cohesion alone can not be considered in isolation fro?
other responsibilities and other principles such as Expert and Low Coupling.
Discussion Like Low Coupling, High Cohesion is a principle to keep in mind during a
design decisions; it is an underlying goal to continually consider. It is an e v a l u ~
tive pattern which a designer applies while evaluating all design decisions.
Grady Booch describes high functional cohesion as existing when the element
of a component (such as a class) "a14 work together to provide some well-bounder
behavior" [Booch94].
Here are some scenarios that illustrate varying degrees of functional cohesion:
1. Very loru cohesion. A class is solely responsible for many things in very dif
ferent functional areas.
o Assume a class exists called RDB-RPC-Interface which is corn
pletely responsible for interacting with relational databases ant
for handling remote procedure calls. These are two vastly differen
functional areas, and each requires lots of supporting code. Th
responsibilities should be split into a family of classes related t
RDB access and a family reIated to RPC suppod.
2. Low cohesion. A class has sole responsibility for a complex task in one h n c
tional area.
18 - GRASP: PATTERNS
FOR ASSIGNING RESPONSIBILITIES