Understanding the Single Responsibility Principle (SRP)

What is the Single Responsibility Principle?

The Single Responsibility Principle (SRP) is the first principle of the SOLID principles of object-oriented design. It states.

“A class should have only one reason to change.”

In simple terms, SRP means that a class should only have one job or function. It should encapsulate only one responsibility and thus should change for only one reason.

Why is SRP Important?

  • Improves code maintainability and readability.
  • Reduces the risk of unintended side effects when changes are made.
  • Makes testing easier by isolating functionality.
  • Encourages reuse and separation of concerns.

Real-World Analogy

Think of a coffee machine. Its job is to make coffee. If we start adding responsibilities like billing the customer, managing inventory, and sending promotional emails, it becomes a tangled mess. Instead, each responsibility should be handled by separate entities.

Violation of SRP

Let's look at an example of how SRP can be violated.

public class Employee {

    public void calculateSalary() {
        // logic to calculate salary
    }
    public void generateReport() {
        // logic to generate report
    }
    public void saveToDatabase() {
        // logic to save to database
    }
}

What's wrong? This class does more than one thing.

  • Business logic: Calculating salary
  • Reporting: Generating reports
  • Persistence: Saving to the database

Each of these responsibilities could change for different reasons, which violates SRP.

How to Fix the Violation?

Break down the responsibilities into separate classes.

// Handles salary calculation
public class SalaryCalculator {
    public void calculateSalary(Employee employee) {
        // calculation logic
    }
}
// Handles report generation
public class ReportGenerator {
    public void generateReport(Employee employee) {
        // report logic
    }
}
// Handles data persistence
public class EmployeeRepository {
    public void save(Employee employee) {
        // save logic
    }
}
// Core entity class
public class Employee {
    private String name;
    private double salary;

    // getters and setters
}

Result: Now each class has one reason to change, making the system easier to understand, test, and maintain.

Benefits of Applying SRP

  • Ease of Maintenance: Smaller classes are easier to maintain and modify.
  • Enhanced Testability: Isolated logic allows for more focused unit tests.
  • Improved Collaboration: Teams can work on different concerns independently.
  • Better Code Reusability: Single-purpose classes are more likely to be reused.

Conclusion

The Single Responsibility Principle promotes clean code architecture by separating concerns into different classes or modules. It leads to a better-organized codebase and forms the foundation for building robust and scalable applications.

Other related articles: Mastering SOLID Principles in Software Design

Up Next
    Ebook Download
    View all
    Learn
    View all