Professional Documents
Culture Documents
Cohesion and Coupling
Cohesion and Coupling
characteristics that have an impact on the resultant design. Ambler [Amb02b] sug-
gests the following guidelines:
10.2.3 Cohesion
In Chapter 8, I described cohesion as the “single-mindedness” of a component.
Within the context of component-level design for object-oriented systems, cohesion
3 It is unlikely that someone from marketing or the customer organization (a nontechnical type)
would examine detailed design information.
pre75977_ch10.qxd 11/27/08 3:46 PM Page 287
FIGURE 10.5
Layer cohesion
Control panel
Detector Phone
Modem
T-com
implies that a component or class encapsulates only attributes and operations that
are closely related to one another and to the class or component itself. Lethbridge
and Laganiére [Let01] define a number of different types of cohesion (listed in order
of the level of the cohesion4):
4 In general, the higher the level of cohesion, the easier the component is to implement, test, and
maintain.)
pre75977_ch10.qxd 11/27/08 3:47 PM Page 288
S AFE H OME
Cohesion in Action
The scene: Jamie’s cubicle. Ed (frowning): Why, all of these little ops can cause
The players: Jamie and Ed—members of the headaches.
SafeHome software engineering team who are working Jamie: The problem with combining them is we lose
on the surveillance function. cohesion, you know, the displayCamera() op won’t be
The conversation: single-minded.
Ed: I have a first-cut design of the camera component. Ed (mildly exasperated): So what? The whole thing
will be less than 100 source lines, max. It’ll be easier to
Jamie: Wanna do a quick review?
implement, I think.
Ed: I guess . . . but really, I’d like your input on
Jamie: And what if marketing decides to change the
something.
way that we represent the view field?
(Jamie gestures for him to continue.)
Ed: I just jump into the displayCamera() op and make the
Ed: We originally defined five operations for camera. mod.
Look . . .
Jamie: What about side effects?
determineType() tells me the type of camera.
Ed: Whaddaya mean?
translateLocation() allows me to move the camera
Jamie: Well, say you make the change but
around the floor plan.
inadvertently create a problem with the ID display.
displayID() gets the camera ID and displays it near the
Ed: I wouldn’t be that sloppy.
camera icon.
Jamie: Maybe not, but what if some support person
displayView() shows me the field of view of the camera
two years from now has to make the mod. He might not
graphically.
understand the op as well as you do, and, who knows, he
displayZoom() shows me the magnification of the cam- might be sloppy.
era graphically.
Ed: So you’re against it?
Ed: I’ve designed each separately, and they’re pretty
Jamie: You’re the designer . . . it’s your decision . . . just
simple operations. So I thought it might be a good idea to
be sure you understand the consequences of low
combine all of the display operations into just one that’s
cohesion.
called displayCamera()—it’ll show the ID, the view, and
the zoom. Whaddaya think? Ed (thinking a moment): Maybe we’ll go with
separate display ops.
Jamie (grimacing): Not sure that’s such a good
idea. Jamie: Good decision.
10.2.4 Coupling
In earlier discussions of analysis and design, I noted that communication and
collaboration are essential elements of any object-oriented system. There is, however,
a darker side to this important (and necessary) characteristic. As the amount of com-
munication and collaboration increases (i.e., as the degree of “connectedness” between
classes increases), the complexity of the system also increases. And as complexity
increases, the difficulty of implementing, testing, and maintaining software grows.
Coupling is a qualitative measure of the degree to which classes are connected to
one another. As classes (and components) become more interdependent, coupling
increases. An important objective in component-level design is to keep coupling as
low as is possible.
pre75977_ch10.qxd 11/27/08 3:47 PM Page 289
Class coupling can manifest itself in a variety of ways. Lethbridge and Laganiére
[Let01] define the following coupling categories:
As the design for each
Content coupling. Occurs when one component “surreptitiously modifies
software component is
elaborated, the focus data that is internal to another component” [Let01]. This violates information
shifts to the design of hiding—a basic design concept.
specific data structures
Common coupling. Occurs when a number of components all make use of
and procedural design
to manipulate the data a global variable. Although this is sometimes necessary (e.g., for establishing
structures. However, default values that are applicable throughout an application), common cou-
don’t forget the pling can lead to uncontrolled error propagation and unforeseen side effects
architecture that must when changes are made.
house the components
or the global data Control coupling. Occurs when operation A() invokes operation B() and
structures that passes a control flag to B. The control flag then “directs” logical flow within
may serve many B. The problem with this form of coupling is that an unrelated change in B
components.
can result in the necessity to change the meaning of the control flag that A
passes. If this is overlooked, an error will result.
Stamp coupling. Occurs when ClassB is declared as a type for an argu-
ment of an operation of ClassA. Because ClassB is now a part of the defini-
tion of ClassA, modifying the system becomes more complex.
Data coupling. Occurs when operations pass long strings of data argu-
ments. The “bandwidth” of communication between classes and components
grows and the complexity of the interface increases. Testing and mainte-
nance are more difficult.
Routine call coupling. Occurs when one operation invokes another. This
level of coupling is common and is often quite necessary. However, it does
increase the connectedness of a system.
Type use coupling. Occurs when component A uses a data type defined in
component B (e.g., this occurs whenever “a class declares an instance vari-
able or a local variable as having another class for its type” [Let01]). If the type
definition changes, every component that uses the definition must also
change.
Inclusion or import coupling. Occurs when component A imports or in-
cludes a package or the content of component B.
External coupling. Occurs when a component communicates or collabo-
rates with infrastructure components (e.g., operating system functions,
database capability, telecommunication functions). Although this type of cou-
pling is necessary, it should be limited to a small number of components or
classes within a system.
S AFE H OME
Coupling in Action
The scene: Shakira’s cubicle. would collaborate directly with the OutgoingCall
The players: Vinod and Shakira—members of the component, well, with an interface to the OutgoingCall
SafeHome software team who are working on the security component.
function. Vinod (pensive): You mean rather than having
The conversation: that collaboration occur out of a component like
ControlPanel or something?
Shakira: I had what I thought was a great idea . . .
then I thought about it a little, and it seemed like a not so Shakira: Yeah . . . but then I said to myself, that
great idea. I finally rejected it, but I just thought I’d run it means that every sensor object will be connected to the
by you. OutgoingCall component, and that means that its
indirectly coupled to the outside world and . . . well, I just
Vinod: Sure. What’s the idea?
thought it made things complicated.
Shakira: Well, each of the sensors recognizes an alarm
Vinod: I agree. In this case, it’s a better idea to let the
condition of some kind, right?
sensor interface pass info to the ControlPanel and let
Vinod (smiling): That’s why we call them sensors, it initiate the outgoing call. Besides, different sensors
Shakira. might result in different phone numbers. You don’t
Shakira (exasperated): Sarcasm, Vinod, you’ve got want the senor to store that information because if it
to work on your interpersonal skills. changes . . .
Shakira: Okay, anyway, I figured . . . why not create an Vinod: Design heuristics for coupling tell us it’s not right.
operation within each sensor object called makeCall() that Shakira: Whatever . . .
10.3 C O N D U C T I N G C O M P O N E N T -L E V E L D E S I G N
Earlier in this chapter I noted that component-level design is elaborative in nature.
uote: You must transform information from requirements and architectural models into
“If I had more a design representation that provides sufficient detail to guide the construction
time, I would have (coding and testing) activity. The following steps represent a typical task set for
written a shorter component-level design, when it is applied for an object-oriented system.
letter.”
Step 1. Identify all design classes that correspond to the problem domain.
Blaise Pascal
Using the requirements and architectural model, each analysis class and architec-
tural component is elaborated as described in Section 10.1.1.