DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

Single Responsibility Principle

The Single Responsibility Principle means that in your code, each "job" or class should do one thing and do it well. This makes your code easier to understand, fix, and use in different parts of your program. Just like tools in a toolbox have specific purposes, your code should have clear and specific responsibilities.

Let's break down the explanation of the Single Responsibility Principle (SRP) into step-by-step actions with a Java code example.

Step 1: Create a Class without a Single Responsibility (Initial Design):

Start with a class that has multiple responsibilities:

public class Employee {
    private String name;
    private double salary;

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

    public void calculateSalary() {
        // Calculate the employee's salary
        this.salary = /* complex calculation */;
    }

    public void saveToDatabase() {
        // Save employee data to a database
        Database.saveEmployee(this);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Identify the Problem:

Recognize that the class violates the Single Responsibility Principle by having more than one responsibility:

  • It calculates the employee's salary.
  • It saves employee data to a database.

Step 3: Apply the Single Responsibility Principle (Solution):

To adhere to the SRP, you need to separate these responsibilities. Here's how:

Step 4: Create Classes with Single Responsibilities (Solution):

Part 1: Create a class responsible for calculating the salary:

public class Employee {
    private String name;
    private double salary;

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

    public double calculateSalary() {
        // Calculate the employee's salary
        return /* complex calculation */;
    }
}
Enter fullscreen mode Exit fullscreen mode

Part 2: Create a class responsible for saving employee data to a database:

public class EmployeeDatabase {
    public void saveToDatabase(Employee employee) {
        // Save employee data to a database
        Database.saveEmployee(employee);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Benefits of the Solution:

By adhering to the Single Responsibility Principle, you've achieved the following benefits:

  • The code is more organized and easier to understand.
  • Each class has a clear and single purpose, making it easier to maintain and modify.
  • Changes in one responsibility (e.g., salary calculation) won't affect the other (e.g., database interaction).
  • Testing is simplified, as you can focus on testing each responsibility independently.

This separation of responsibilities enhances code quality and maintainability.

"Your feedback and ideas are invaluable โ€“ drop a comment, and let's make this even better!"

๐Ÿ˜ If you enjoy the content, please ๐Ÿ‘ like, ๐Ÿ”„ share, and ๐Ÿ‘ฃ follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Top comments (0)