Design Concepts  «Prev  Next»
Lesson 5Coupling
ObjectiveDefine coupling.

Object Coupling Decisions

Problem

Do I put this function in this object or that object? If I put it in that object, then the first object always needs to ask the second object for help. If I put it in the first object, then the first object has low cohesion. What do I do?

Coupling

Coupling is a measure of the degree of dependency between objects.
Dependency means that one object requires the data or the functionality owned by another object. Loose coupling means a low degree of dependency, and tight coupling means a high degree of dependency.
If a change in one object requires a change in another object, the second object is dependent on the first. For example, if the interface to a server changes, the client application probably will not work. The client application depends on the server to function properly.

Consequences

Coupling has a direct effect on system maintenance. Tight coupling results in a ripple effect, that is, a change in one object requires changes (or, at the very least, testing) of all the associated objects.

Responsibility

Loose coupling can be achieved by assigning to an object only the behaviors that closely map to the object’s purpose (high cohesion). Avoid including behaviors simply because they are needed for a process that the object participates in.

Cohesion and coupling: balance is the key to success

Cohesion and coupling should always be evaluated together. Loose coupling can be achieved easily by very low cohesion, that is, by cramming everything into one object so it does not need help from any other objects. High cohesion can result in too many tiny objects that cannot get anything done without talking to a lot of other tiny objects. The communication overhead can destroy the performance of the application. The optimum solution is a compromise between high cohesion and loose coupling.
These two concepts, cohesion and coupling, are enabling factors in the creation of design patterns. In a design pattern, every object has a specific responsibility, that is, high cohesion. The collection of objects has a predictable pattern of collaboration or communication. The pattern of collaboration is based on the specific nature of the coupling between the objects, that is, the help that each object requires from other objects.

Loose Tight Coupling - Exercise

Click the Exercise link below to determine instances of loose and tight coupling.
Loose Tight Coupling - Exercise