Grokking SOLID Design Principles
Ask Author
Back to course home

0% completed

Cohesion and its Relation to the Single Responsibility Principle (SRP)
Table of Contents

What is Cohesion?

General Example: Garbage Dump vs. Organized Dustbins

Low Cohesion in Code: Single Class with Multiple Responsibilities

Increasing Cohesion: Splitting Responsibilities into Separate Classes

Cohesion and SRP

What is Cohesion?

Cohesion refers to how closely related and focused the responsibilities of a class, module, or method are.

A highly cohesive class will have methods that work together to achieve a single purpose. On the other hand, low cohesion occurs when a class has unrelated responsibilities bundled together, which makes it harder to maintain and understand.

General Example: Garbage Dump vs. Organized Dustbins

Imagine you have a garbage dump where all kinds of waste are thrown together—plastic, paper, food waste, and more. This is like a class with low cohesion, where everything is mixed and there’s no clear purpose for the class. It’s hard to manage and sort out when you need something specific.

Image

Now, think about separate dustbins for each type of waste as shown in image: one for plastic, one for paper, and one for food waste. This is a class with high cohesion. Each dustbin (or class) has a clear responsibility and manages only the type of waste it’s meant for.

By organizing waste into specific bins, the system is much easier to manage, and that’s exactly what cohesion helps achieve in code.

Low Cohesion in Code: Single Class with Multiple Responsibilities

Let’s take a look at a coding example with low cohesion. Here, we have a single class that handles user data and file management, all in one place.

Python3
Python3
. . . .

In this code, the UserManager class handles user management and file operations. It has low cohesion because it tries to handle too many responsibilities that aren’t closely related.

Increasing Cohesion: Splitting Responsibilities into Separate Classes

To improve cohesion and follow the SRP, we can split this class into smaller, more focused classes. One class will handle user management, and another will take care of file operations.

Python3
Python3
. . . .

Now, the UserManager class is only responsible for managing users, while the FileManager class deals with file operations. Each class has a clear purpose and focuses on one area, leading to higher cohesion.

Cohesion and SRP

To follow the Single Responsibility Principle, your classes need high cohesion. Why? Because high cohesion means that a class focuses on a single responsibility or task, which is exactly what SRP demands.

  • Low cohesion often results in a class with multiple unrelated responsibilities, violating the SRP.
  • High cohesion ensures that each class or module is centered around one responsibility, making the code easier to maintain and extend.

By organizing your code to have high cohesion, you naturally follow the SRP, as each class or component will focus on a single, clearly defined responsibility.

Mark as Completed

Table of Contents

What is Cohesion?

General Example: Garbage Dump vs. Organized Dustbins

Low Cohesion in Code: Single Class with Multiple Responsibilities

Increasing Cohesion: Splitting Responsibilities into Separate Classes

Cohesion and SRP