SOLID-- way to design system

Abhishek RajAbhishek Raj
2 min read

S — Single Responsibility Principle (SRP)

A class should have only one reason to change.

  • Meaning: A class should do only one job or have one responsibility.

  • Why: If a class does multiple things, changes in one part can affect others unnecessarily.

💡 What does this mean?

Think of a class like a department in a company. A department should focus on one area: HR does hiring, Finance does budgeting, and IT handles tech. If one department is trying to do everything, it becomes a mess.

In code, a class doing too much—handling database, business logic, and UI—becomes hard to maintain and test. If you need to change one part, you risk breaking another.

📦 Example (Bad SRP):

class MathProcessor {
    int add(int a, int b) {
        return a + b;
    }

    void printResult(int result) {
        System.out.println("Result: " + result);
    }
}

✅ Why is this bad?
This one class does two things:

Calculates a result
Prints the result

📦 Example (Good SRP ):

class Calculator {
    int add(int a, int b) {
        return a + b;
    }
}

class ResultPrinter {
    void print(int result) {
        System.out.println("Result: " + result);
    }
}

✅ Why is this good?
seperate class for things:-

Calculator only does math
ResultPrinter only prints
Easy to test and maintain
Each class has one reason to change

🔄 Why is this helpful?

  • Easier to maintain and debug

  • Easier to test (unit tests for just one thing)

  • Encourages cleaner, modular code

🧱 How do you know if you're violating SRP?

Ask yourself:

  • Is this class doing more than one type of thing?

  • Could different people or requirements cause this class to change?

  • Is it handling logic + UI + database or other mixed concerns?

If yes → you’re likely breaking SRP.

0
Subscribe to my newsletter

Read articles from Abhishek Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Raj
Abhishek Raj