Download - Cohesion and Coupling
![Page 1: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/1.jpg)
Cohesion and Coupling
01/01/10
![Page 2: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/2.jpg)
Coupling
• The amount of interaction among components in a system
![Page 3: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/3.jpg)
Indications of Coupling
![Page 4: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/4.jpg)
Coupling Types• Content coupling• Common coupling• External coupling• Control coupling• Stamp coupling• Data coupling• Uncoupled
![Page 5: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/5.jpg)
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
![Page 6: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/6.jpg)
External Coupling
• Two components share something externally imposed
![Page 7: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/7.jpg)
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
![Page 8: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/8.jpg)
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
![Page 9: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/9.jpg)
Data Coupling
• Component A passes data to component B• Not the same as Stamp Coupling:
– Data (a representation), not a data structure (an implementation).
![Page 10: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/10.jpg)
Example
The print routine takes the customer name, address, and billing information as arguments.
Customer billing system
![Page 11: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/11.jpg)
Uncoupled
• Completely uncoupled components are not systems.
• Systems are made of interacting components.
![Page 12: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/12.jpg)
Key Idea in Object-Oriented Programming
![Page 13: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/13.jpg)
Consequences of Coupling
![Page 14: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/14.jpg)
Q R S T U
p
q
r
s
t
Coupling between pairs of modules
![Page 15: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/15.jpg)
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.
![Page 16: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/16.jpg)
Types of Cohesion: low to high
• Coincidental low• Logical• Temporal• Procedural• Communicational• Sequential • Functional high
![Page 17: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/17.jpg)
Coincidental Cohesion
• Accidental• Worst form
![Page 18: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/18.jpg)
Logical Cohesion
• Several logically related elements are in the same component.
• Not related functionally, but related logically.
![Page 19: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/19.jpg)
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.
![Page 20: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/20.jpg)
Temporal Cohesion
• Elements of a component are related by timing
• Elements are grouped by when they are processed
![Page 21: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/21.jpg)
Procedural Cohesion
• Elements of a component are related only to ensure a particular order of execution
• Common to pass partial results
![Page 22: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/22.jpg)
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.
![Page 23: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/23.jpg)
Example
• Update record in data base and send it to the printer.
• database.Update (record).
• record.Print().
![Page 24: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/24.jpg)
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
![Page 25: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/25.jpg)
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
![Page 26: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/26.jpg)
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.
![Page 27: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/27.jpg)
Techniques for Improving Design
• Increase cohesion
• Design by contract
• Prototype design
![Page 28: Cohesion and Coupling](https://reader035.vdocuments.net/reader035/viewer/2022062305/56815d4a550346895dcb5254/html5/thumbnails/28.jpg)
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