Download presentation
Presentation is loading. Please wait.
Published byÍΘεριστής Αλαβάνος Modified over 6 years ago
1
Cohesion and Coupling Chapter 5, Pfleeger 01/01/10
2
Coupling The amount of interaction among components in a system
What is it?
3
Indications of Coupling
4
Coupling Types Content coupling Common coupling External coupling
Control coupling Stamp coupling Data coupling Uncoupled Pp220+ pfleeger 4
5
Content and Common Coupling
The components share data via direct access to data stores: One component depends on the internal workings of another: Usually a poor design choice because Pp220+ pfleeger
6
External Coupling Two components share something externally imposed
Pp220+ pfleeger 6
7
Control Coupling Component passes control parameters to coupled components. May be either good or bad, depending on situation. Bad if parameters indicate completely different behavior Good if parameters allow factoring and reuse of functionality Example of good: Sort that takes a comparison function as an argument. The sort function is clearly defined: return a list in sorted order, where sorted is determined by a parameter.
8
Stamp Coupling Component A passes a data structure to component B
B only uses part of the data Requires B to know how to manipulate the data structure (eg needs to know about implementation) B has access to more information that it needs Be sure you know the difference between the insightful designer and the lazy programmer. Know what tradeoffs you are making in your design.
9
Data Coupling Component A passes data to component B
Not the same as Stamp Coupling: Data (a representation), not a data structure (an implementation).
10
Example Customer billing system The print routine takes the customer name, address, and billing information as arguments.
11
Uncoupled Completely uncoupled components are not systems.
Systems are made of interacting components.
12
Key Idea in Object-Oriented Programming
13
Consequences of Coupling
14
Coupling between pairs of modules
Q R S T U p q r s t
15
Cohesion The degree to which all elements of a component are directed towards a single task The degree to which all elements directed towards a task are contained in a single component The degree to which all responsibilities of a single class are related Note: We are talking about the contents of a single component. What is it
16
Types of Cohesion: low to high
Coincidental low Logical Temporal Procedural Communicational Sequential Functional high Pp 223+ pfleeger
17
Coincidental Cohesion
Accidental Worst form
18
Logical Cohesion Several logically related elements are in the same component. Not related functionally, but related logically.
19
Example A component reads inputs from tape, disk, and network.
All the code for these functions are in the same component. Operations are related logically, but the functions are significantly different.
20
Temporal Cohesion Elements of a component are related by timing
Elements are grouped by when they are processed
21
Procedural Cohesion Elements of a component are related only to ensure a particular order of execution Common to pass partial results
22
Communicational Cohesion
Elements of a component are related by the data that they process Component performs a series of actions related by a sequence of steps to be followed. All actions are performed on the same data.
23
Example Update record in data base and send it to the printer.
database.Update (record). record.Print(). I don’t even want to think about it.
24
Sequential Cohesion The output of one component is the input to another. Good situation Data flows between components (different from procedural cohesion) Occurs naturally in functional programming languages
25
Informational (Data) Cohesion
Module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data. Difference from logical cohesion
26
Functional Cohesion Every essential element to a computation is contained in the component. Every element in the component is essential to the computation. Ideal situation.
27
Techniques for Improving Design
Increase cohesion Design by contract Prototype design
28
Problem: Classify cohesion for each module
Compute average daily temperatures at various sites Initialize sums and open files Create new temperature record Store temperature record Close files and print average temperatures Read in site, time, and temperature Store record for specific site Edit site, time, or temperature field
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.