780 likes | 2.25k Views
Cohesion and Coupling. 01/01/10. Coupling. The amount of interaction among components in a system. Indications of Coupling. Coupling Types. Content coupling Common coupling External coupling Control coupling Stamp coupling Data coupling Uncoupled. Content and Common Coupling.
E N D
Cohesion and Coupling 01/01/10
Coupling • The amount of interaction among components in a system
Coupling Types Content coupling Common coupling External coupling Control coupling Stamp coupling Data coupling Uncoupled
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
External Coupling Two components share something externally imposed
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
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
Data Coupling • Component A passes data to component B • Not the same as Stamp Coupling: • Data (a representation), not a data structure (an implementation).
The print routine takes the customer name, address, and billing information as arguments. Example Customer billing system
Uncoupled Completely uncoupled components are not systems. Systems are made of interacting components.
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.
Types of Cohesion: low to high • Coincidental low • Logical • Temporal • Procedural • Communicational • Sequential • Functional high
Coincidental Cohesion • Accidental • Worst form
Logical Cohesion • Several logically related elements are in the same component. • Not related functionally, but related logically.
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. Example
Temporal Cohesion • Elements of a component are related by timing • Elements are grouped by when they are processed
Procedural Cohesion • Elements of a component are related only to ensure a particular order of execution • Common to pass partial results
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.
Update record in data base and send it to the printer. database.Update (record). record.Print(). Example
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
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
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.
Techniques for Improving Design • Increase cohesion • Design by contract • Prototype design
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