Global Reach India UAE USA UK Australia
Single Responsibility Principle (SRP) Explained – Stepwise Guide

Single Responsibility Principle (SRP) – Complete Guide

What is SRP?

The Single Responsibility Principle (SRP) is a core concept in SOLID programming. It states that every class or module should have only one responsibility, meaning only one reason to change. Following SRP makes code easier to maintain, test, and extend.

Why SRP is Important

  • Reduces complexity and coupling.
  • Makes code easier to read and maintain.
  • Facilitates testing and debugging.
  • Prevents unintended side effects when changing code.

Stepwise Implementation of SRP

  1. Step 1 – Identify Responsibilities: Review your class or module to see all tasks it performs.
  2. Step 2 – Separate Responsibilities: Divide the tasks into different logical responsibilities.
  3. Step 3 – Create Single Responsibility Classes: Assign each responsibility to a dedicated class.
  4. Step 4 – Refactor Code: Move relevant methods and data to the new classes.
  5. Step 5 – Test Functionality: Ensure each class performs its single task correctly.

Example: SRP in Action

Suppose we have an application that needs to:

  • Send emails
  • Fetch employee data
  • Calculate salaries

Instead of one class handling all three, we separate responsibilities:

class EmailSender {
    public void sendEmail(String recipient, String message) {
        // logic to send email
    }
}

class DataFetcher {
    public Employee getEmployeeData(int id) {
        // logic to fetch employee data
    }
}

class SalaryCalculator {
    public double calculateSalary(Employee emp) {
        // logic to calculate salary
    }
}
      

This way, each class has a single responsibility. Modifying email logic won’t affect salary calculation or data fetching.

Top 10 SRP & SOLID Interview Questions

1. What is Single Responsibility Principle (SRP)?
SRP states that a class should have only one responsibility or reason to change, making code maintainable and easier to test.
2. Why is SRP important?
It reduces complexity, improves maintainability, and prevents unintended side effects when changing code.
3. Can you give an SRP example?
PaymentProcessor handles payments, EmailSender handles emails, DataFetcher handles data. Each class has a single responsibility.
4. How to implement SRP stepwise?
Step 1: Identify responsibilities, Step 2: Separate responsibilities, Step 3: Create dedicated classes, Step 4: Refactor code, Step 5: Test functionality.
5. Does SRP apply only to classes?
No, SRP applies to functions, methods, modules, and classes. Each should have a single purpose.
6. What are SRP benefits?
Better maintainability, easier testing, reduced bugs, cleaner architecture, and scalable code.
7. What happens if SRP is violated?
Classes with multiple responsibilities become complex, harder to test, and prone to bugs when modified.
8. How to refactor a class violating SRP?
Identify separate responsibilities and move them to distinct classes, leaving one responsibility per class.
9. Can SRP work with other SOLID principles?
Yes, SRP works with OCP, LSP, ISP, and DIP for clean, modular, and maintainable code.
10. Example of SRP in real life?
A HR system: SalaryCalculator calculates salary, DataFetcher fetches employee data, EmailSender sends payslips. Each class has a single responsibility.
⚠️ Important Notice: SupportDeskWorld is an independent informational platform. We provide verified, publicly available guides, tutorials, and awareness content. We do not offer direct services, financial advice, legal work, repairs, or government assistance. For official inquiries, please use our Contact Page.
Scroll to Top