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

Coupling and Object Coupling Decisions

To determine which method belongs to a class when defining "coupling" in UML modeling, consider the following factors:
  1. Cohesion: Cohesion measures the degree to which a method is related to the class it belongs to. A method with high cohesion is strongly related to the class and its responsibilities. In contrast, a method with low cohesion is loosely related to the class and may be better suited for another class.
  2. Coupling: Coupling measures the degree to which a method depends on other methods or classes. A method with high coupling is heavily dependent on other methods or classes, while a method with low coupling is relatively independent.
  3. Responsibility: A method should fulfill a specific responsibility within the class. The responsibility of a method defines its purpose and how it contributes to the overall functionality of the class.
  4. Encapsulation: Encapsulation is the bundling of data and methods into a single unit, such as a class. Encapsulation helps to keep the internal details of a class hidden from other parts of the system. Methods that access or modify private data members of a class should generally be encapsulated within the same class.
  5. Modularity: Modularity is the degree to which a system can be decomposed into independent, reusable components. Methods that are highly modular are easier to understand, maintain, and reuse in other parts of the system.

Based on these factors, you can determine which methods belong to a class by considering the following:
  1. Methods that are highly cohesive with the class: These methods are strongly related to the class and its responsibilities. They should be included in the class.
  2. Methods that have low coupling with other methods or classes: These methods are relatively independent and do not rely heavily on other parts of the system. They should be included in the class if they are closely related to the class's responsibilities.
  3. Methods that fulfill a specific responsibility within the class: These methods have a clear purpose and contribute to the overall functionality of the class. They should be included in the class.
  4. Methods that access or modify private data members of the class: These methods should generally be encapsulated within the same class to maintain encapsulation.
  5. Methods that are highly modular and reusable: These methods are easier to understand, maintain, and reuse in other parts of the system. They should be included in the class if they are closely related to the class's responsibilities.

By considering these factors, you can make informed decisions about which methods belong to a class when defining "coupling" in UML modeling.

Architect's Challenge when determining Coupling

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 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.
  1. 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.
  2. 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, 1) cohesion and 2)coupling, are the 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