Knowledge Guide
HomeOO & Low-Level DesignSOLID Principles

Cohesion and its Relation to the Single Responsibility Principle 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
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.

java
public class UserManager {
    public void addUser(String user) {
        // Logic to add a user
    }

    public void deleteUser(String user) {
        // Logic to delete a user
    }

    public void writeToFile(String data) {
        // Logic to write data to a file
    }

    public String readFromFile(String fileName) {
        // Logic to read data from a file
        return fileName;
    }
}

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.

java
public class UserManager {
    public void addUser(String user) {
        // Logic to add a user
    }

    public void deleteUser(String user) {
        // Logic to delete a user
    }
}

public class FileManager {
    public void writeToFile(String data) {
        // Logic to write data to a file
    }

    public String readFromFile(String fileName) {
        // Logic to read data from a file
        return fileName;
    }
}

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.

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.

🤖 Don't fully get this? Learn it with Claude

Stuck on Cohesion and its Relation to the Single Responsibility Principle SRP? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Cohesion and its Relation to the Single Responsibility Principle SRP** (OO & Low-Level Design) and want to truly understand it. Explain Cohesion and its Relation to the Single Responsibility Principle SRP from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Cohesion and its Relation to the Single Responsibility Principle SRP** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Cohesion and its Relation to the Single Responsibility Principle SRP** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Cohesion and its Relation to the Single Responsibility Principle SRP** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes