cohesion and coupling

28
Cohesion and Coupling 01/01/1 0

Upload: ciqala

Post on 11-Feb-2016

237 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Cohesion and Coupling

Cohesion and Coupling

01/01/10

Page 2: Cohesion and Coupling

Coupling

• The amount of interaction among components in a system

Page 3: Cohesion and Coupling

Indications of Coupling

Page 4: Cohesion and Coupling

Coupling Types• Content coupling• Common coupling• External coupling• Control coupling• Stamp coupling• Data coupling• Uncoupled

Page 5: Cohesion and Coupling

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

External Coupling

• Two components share something externally imposed

Page 7: Cohesion and Coupling

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

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

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

Example

The print routine takes the customer name, address, and billing information as arguments.

Customer billing system

Page 11: Cohesion and Coupling

Uncoupled

• Completely uncoupled components are not systems.

• Systems are made of interacting components.

Page 12: Cohesion and Coupling

Key Idea in Object-Oriented Programming

Page 13: Cohesion and Coupling

Consequences of Coupling

Page 14: Cohesion and Coupling

Q R S T U

p

q

r

s

t

Coupling between pairs of modules

Page 15: Cohesion and Coupling

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

Types of Cohesion: low to high

• Coincidental low• Logical• Temporal• Procedural• Communicational• Sequential • Functional high

Page 17: Cohesion and Coupling

Coincidental Cohesion

• Accidental• Worst form

Page 18: Cohesion and Coupling

Logical Cohesion

• Several logically related elements are in the same component.

• Not related functionally, but related logically.

Page 19: Cohesion and Coupling

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

Temporal Cohesion

• Elements of a component are related by timing

• Elements are grouped by when they are processed

Page 21: Cohesion and Coupling

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

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

Example

• Update record in data base and send it to the printer.

• database.Update (record).

• record.Print().

Page 24: Cohesion and Coupling

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

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

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

Techniques for Improving Design

• Increase cohesion

• Design by contract

• Prototype design

Page 28: Cohesion and Coupling

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