Coupling Cohesion
Coupling Cohesion
Coupling
Definition of Coupling
The degree of interdependence between two or more components.
Reducded coupling Reduced Complexity
Definition of Coupling
The fewer the connection, the less chance there is for the ripple effect.
Ideally, changing one component should not require a change in another. Should not have to understand details of one component in order to change another (c.f. principle of locality).
Coupling Principles
Create narrow connections Create direct Connections Create local connections Create obvious connections Create flexible connections
Remote Connection
Coupled through global data. May be hundreds of lines removed from a call point. May be remote in time. (C) Michelle Lee, 1999
Obvious Connections
Easier to understand the obscure connections
Avoid strange or unnatural connections.
E.g storing area code at the end of phone number.
10
Flexible Connections
Systems evolve over time.
information being passed changes. May want to switch to a different logical or physical devices.
11
Types of Coupling
NORMAL
Data Stamp Control
Acceptable
COMMON CONTENT
Unacceptable
(C) Michelle Lee, 1999 12
Normal Coupling
Two components, A and B, are normally coupled if
A calls B B returns to A All information passed between them is by parameters in the call (or a return).
Data Coupling
Two components are data coupled if they communicate through parameters. Each parameter is an elementary piece of data. Most common form of coupling. Harmless if number of parameters is kept to a minimum.
(C) Michelle Lee, 1999 14
Data Coupling
Keep interfaces as narrow as possible. Avoid tramp data:
Data passed from component to component that is unwanted and meaningless to most.
15
Stamp Coupling
Two normally coupled components are stamp coupled if one passes to the other a complete piece of data - one with meaningful internal structure.
E.g customer records
16
Control Coupling
Two components are control coupled if one passes to the other a piece of information intended to control the internal logic of another.
E.g. flags
Downside: Caller must understand some of the details about how the logic of the callee is organized.
(C) Michelle Lee, 1999 18
19
10
22
11
24
12
Class Coupling
The coupling between tow classes is a measure of how much they depend on each other (degree of dependency) Addresses tow related issues
How much do I need to understand about related classes in order to understand a particular class? If changes are made in one class, what is the potential impact with respect to other classes in the system?
(C) Michelle Lee, 1999 26
13
Class Coupling
Coupling chan range from none (independent classes) to very strong (e.g. friend classes in C++) Dependency between classes can be in one direction (Class A dependents on B, but not conversely) or both directions Direct correlation between low coupling and reusability General goal: Minimize the amount of coupling between classes
(C) Michelle Lee, 1999 27
28
14
30
15
Inheritance forces you to look in potentially many places in order to understand a component.
(C) Michelle Lee, 1999 31
Type Coupling
32
16
Context Coupling
Classes are like mini systems:
Have their own set of methods Encapsulate a set of state variables. But, classes are more than this, they define types!
Type Coupling
Coupling that results from the use of common type declarations.
Probably the most common form of coupling in an object-oriented program.
34
17
35
Content Coupling
Internal Representation Coupling: one class has direct access to the private attribute values (instance variables) of another class
Very strong coupling and usually undesirable Example: friend classes in C++ Possible application: container class and an associated iterator class (two highly interdependent classes which work closely together to achieve a common goal)
(C) Michelle Lee, 1999 36
18
Cohesion
37
Cohesion
The cohesion of a component is a measure of how well it fits together. A component should implement a single logical function or should implement a single logical entity.
38
19
Best Maintainability
39
Logical association
Components which perform similar functions such as input, error handling, etc. are put together in a simple component.
40
20
41
21
Drawbacks:
Tend to be composed of pieces of functions that have little relationship to one another (except for order and time that they are carried out).
(C) Michelle Lee, 1999 43
Sequential cohesion
The output from one element in the component serves as input for some other element Good coupling, easily maintained.
(C) Michelle Lee, 1999 44
22
Functional Cohension
Functional cohesion
The parts of a functionally cohesive component contribute to a single purpose or problem. Each part of the component is necessary for the execution of a single function Single-purpose
Low-level (methods, classes) Medium-level (packages, class categories) High-level (systems, sub-systems)
(C) Michelle Lee, 1999 45
46
23
1 Page-Jones, M. (1988). The Practical Guide to Structured Systems Design. Englewoold Cliffs, New Jersey, Yourdon Press.
47
Object-Oriented Cohesion
48
24
Class Cohesion
Class cohesion
Each operation provides function which allows the attributes of the class to be modified, inspected or used as a basis for service provision.
A cohesive object is one where a single entity is represented and all of the operations on that entity are included with the object
(C) Michelle Lee, 1999
49
Class Cohesion
Cohesion is a measure of strength of connectivity of the items within a class (binding strength) Whereas coupling describe the relationships between classes, cohesion describes the relationships within the class. Address the purpose of the class: Do the items (attributes, operations, etc.) of a class work together toward a single, common purpose? (C) Michelle Lee, 1999 50
25
Class Cohesive
Highly cohesive classes are usually more easily understood and and less susceptible to change (more stable) General goal: strong class cohesion
51
52
26
54
27
55
Class cohesion
Cohesion means a unit represents a single part of the problem solution. The inheritance promotes reusability but reduce cohesion
It is no longer consider that object as a separate unit. All super-classes also have to be inspected if the objects functionality is to be understood completely.
(C) Michelle Lee, 1999 56
28