Single Responsibility Principle (SRP) in SOLID with Java Example

Writing clean, maintainable, and scalable code is crucial in software development. One key principle that helps achieve this is the Single Responsibility Principle (SRP), one of the five SOLID principles of object-oriented design.


What is the Single Responsibility Principle?

The Single Responsibility Principle states that:

"A class should have only one reason to change."

This means that each class should have only one responsibility and one focus. If a class handles multiple responsibilities, it becomes more complex, more challenging to test, and more difficult to maintain.

Following SRP, we can keep our code modular, making it easier to understand, test, and extend.


Example: Violating the Single Responsibility Principle

Let’s consider an example where a class violates the Single Responsibility Principle:

class Employee {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    // Responsibility 1: Calculating employee's salary
    public double calculateBonus() {
        return salary * 0.10; // 10% bonus
    }

    // Responsibility 2: Saving employee details to a file
    public void saveToFile() {
        System.out.println("Saving employee data to a file...");
    }
}

What is wrong with this code?

The Employee class has two responsibilities:

  1. Business logic (Calculating salary and bonus)
  2. Persistence logic (Saving data to a file)

If we need to change how the salary is calculated, we modify the same class that handles file storage. This violates SRP and makes the class harder to maintain.


Applying the Single Responsibility Principle

To follow SRP, we should separate the concerns. We can create two separate classes:

  1. Employee – Only contains employee-related data
  2. SalaryCalculator – Handles salary calculations
  3. EmployeePersistence – Handles saving employee data

Here’s the refactored code:

// Employee class now has only one responsibility: storing employee details
class Employee {
    private String name;
    private double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}

// SalaryCalculator is responsible for salary-related operations
class SalaryCalculator {
    public double calculateBonus(Employee employee) {
        return employee.getSalary() * 0.10; // 10% bonus
    }
}

// EmployeePersistence is responsible for saving employee data
class EmployeePersistence {
    public void saveToFile(Employee employee) {
        System.out.println("Saving employee " + employee.getName() + " data to a file...");
    }
}

Benefits of Applying the Single Responsibility Principle

  1. Improved Maintainability

    • Changes to salary calculations do not affect file storage logic.
  2. Better Readability and Modularity

    • Code is easier to understand and modify.
  3. Easier Unit Testing

    • We can test salary calculations separately from persistence operations.
  4. Scalability

    • If we need to store employee data in a database instead of a file, we only modify EmployeePersistence, leaving other classes unchanged.

Summary

The Single Responsibility Principle (SRP) helps keep our Java applications modular, clean, and easy to maintain. By ensuring that each class has only one reason to change, we can write better, more scalable, and testable software.

Applying SRP reduces complexity, improves reusability, and enhances collaboration in software projects. Always keep in mind:

"A class should do one thing and do it well."


Would you like me to add more real-world examples or expand on any section? 🚀

Thanks for reading! 🎉 I'd love to know what you think about the article. Did it resonate with you? 💭 Any suggestions for improvement? I’m always open to hearing your feedback so that I can improve my posts! 👇🚀. Happy coding! 💻

Related Posts:

0 comments:

Post a Comment