OOPortal OOPortal


Model Refinement   «Prev  Next»
Lesson 4Cohesion and coupling revisited
ObjectiveReinforce the principles of cohesion and coupling as they apply to class modeling.

Cohesion and Coupling Revisited

Cohesion and coupling are measures of quality for your model. As such, they should be applied to every activity of the modeling process. Like encapsulation, cohesion and coupling may and should be applied to every level your model, not just to individual class design.

Key to achieving high cohesion

The key to achieving high cohesion and low coupling is defining purpose. The best way I have found to understand this concept is to think in terms of human interaction. People and organizations work best when they have a single, well-defined purpose. When the purpose is poorly defined, people have a hard time setting priorities, developing a plan of action, and coordinating unrelated tasks.
Cohesion Business Example

Systems, applications, components, and objects react in the same way. Actually, it is the developers who react in the same way. Development becomes increasingly complex as you attempt to put more and more functionality into the same component. More and more code is required to track object state, juggle priorities, and manage the execution of loosely related or unrelated activities.

Refactoring for higher cohesion

Refactoring for higher cohesion and looser coupling is usually a matter reassigning responsibilities and the associated functionality.
The View MouseOver links below will show you two examples of refactoring. In the first example, the initial job description for a Software Developer is refactored so that the skill and tool sets in the initial definition are distributed into three distinct job classifications. In the second example, the definitions for a security user interface and a security client are refactored so that their responsibilities are divided in such a way so as to allow the user interface to change without disturbing or even touching the logic behind it.

Initial Job Description Example 1: View the MouseOver below to view the initial job description for a Software Developer.

Refactoring Revisited

In this definition the distinct skill and tool sets are distributed into three distinct classifications.

Example 2: View the Mouse diagram below to view to view a refactored version of the job description for a Software Developer.
In this definition the user interface contains the screen elements needed to obtain the user input and the logic to prevent unlimited attempts

Initial Interface Definition
Example 3: View the Mouse diagram below to view the initial definition for a Security User Interface and a Security Client Application.
This definition divides the responsibilities into a) user data entry and b) user access, c) the security system, and d) the database behind the security system. This approach allows the user interface to change without disturbing or even touching the logic behind it. The interface is completely ignorant of how it is being used. It merely provided fields to receive and display information. In fact the same interface could be used at many different levels through the system, e.g. application security, system security, and function level security

Refactored Definition for Interface
Example 2: Click the View MouseOver button to see a refactored definition for a Security User Interface and a Security Client Application
You can see from the examples that refactoring based on cohesion and coupling typically results in more objects that are also smaller and simpler, and consequently far more reusable.

Cohesion Coupling - Exercise

Click the Exercise link below to refactor examples taken from the course project to improve cohesion and reduce coupling.
Cohesion Coupling - Exercise