Understanding Coupling and Cohesion in C++: A Beginner's Practice Guide

Introduction

In object-oriented software design, Coupling and Cohesion are two important principles used to evaluate the quality of a software system. Well-designed software aims to achieve High Cohesion and Low (Loose) Coupling.

Cohesion measures how closely related the responsibilities of a single class are. A highly cohesive class performs one specific task, while a class with low cohesion performs multiple unrelated tasks.

Coupling measures the level of dependency between different classes. Loosely coupled classes interact through well-defined interfaces and can be modified independently, whereas tightly coupled classes depend heavily on each other's implementation details.

Following these principles results in software that is easier to understand, maintain, test, and extend.

This document presents practical examples demonstrating both cohesion and coupling in C++.

Cohesion: Low Cohesion vs. High Cohesion

Cohesion refers to how well the members of a class belong together. A class should have one clear responsibility.

Program (Low Cohesion)


Program (High Cohesion)


Explanation

In the first version, the LowCohesionUser class performs several unrelated responsibilities. It stores user data, encrypts passwords, and also manages the presentation of information on the screen.

Combining multiple responsibilities into one class reduces cohesion and makes the class more difficult to maintain.

In the improved version, each class performs only one specific task. The AccountUser class stores user information, the PasswordCryptoEngine handles password encryption, and the DashboardRenderer manages presentation.

Since every class has a single responsibility, the design has high cohesion.

Coupling: Tight Coupling vs. Loose Coupling

Coupling measures how strongly one class depends on another. Software should minimize dependencies between classes whenever possible.

Program (Tight Coupling)


Program (Loose Coupling)


Explanation

In the first version, the TightOrderShipment class depends directly on the FedExDelivery class. If the company decides to use another delivery service, the shipment class must be modified.

This creates tight coupling between the two classes.

In the improved version, the shipment class communicates through the IShippingProvider interface rather than depending on a specific courier implementation.

Different shipping providers such as FedExService and UPSService implement the same interface, allowing them to be used interchangeably without changing the shipment class.

This design follows the principle of loose coupling and improves flexibility and maintainability.

Characteristics of Coupling and Cohesion

PropertyDescription
High CohesionA class performs one specific responsibility and all its members are closely related.
Low CohesionA class performs multiple unrelated responsibilities.
Loose CouplingClasses communicate through interfaces or abstractions with minimal dependency.
Tight CouplingClasses depend directly on each other's implementation details.
Primary GoalDesign software with High Cohesion and Low Coupling for better maintainability and flexibility.

Conclusion

Coupling and Cohesion are two essential principles of object-oriented software design. High cohesion ensures that every class has a single, well-defined responsibility, making the code easier to understand and maintain. Loose coupling minimizes dependencies between classes, allowing components to be modified or replaced without affecting the rest of the system. As demonstrated in the examples, following these principles results in modular, reusable, flexible, and maintainable C++ applications.