DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

2

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay