SRP - Single Responsibility Principle

A class should have one and only one reason to change, meaning that a class should have only one job.

The Single Responsibility Principle (SRP) states that a class or module should have one, and only one reason to change. This principle gives us both a definition of responsibility and guidelines for class size. Classes should have one responsibility—one reason to change.

According to this principle, we have to separate this class into many separate classes, each of which has only one responsibility. Although more classes will be generated, it is easier to understand, easy to reuse, and smaller classes so fewer bugs.

Some examples of SRPs to consider that can be segregated include Persistence, Validation, Messages, Error Handling, Logging, Formatting, Parsing, and Mapping, ...

Cohesion is how various parts of a software component are related. Higher cohesion helps to achieve better adherence to the Single Responsibility Principle.

Example

To understand this principle, let's see an example of a class that violates this principle as follows:

class UserService {
    public User createUser(UserDto user) {
        // INSERT INTO user(...);
    }

    public boolean isStrongPassword(String password) {
        // Matcher.match("Strong password Regex pattern");
    }

    public void sendEmail(String toEmail, String content) {
        // Get email config
        // Create Gmail object
        // Send email
    }

    public void log(String message) {
        // Console.log(....);
    }
}

As you can see, this class performs a lot of different responsibilities: creating use, validation, sending emails, logging, etc. When we just need to change the way we create the user, get the data from DB, or change the authentication way… we will have to modify this class, which has to get bigger and bigger in the future. Very difficult to maintain, upgrade, fix, test, etc.

According to this principle, we must separate this class into many separate classes, each of which only does a single task, e.g. UserRepository, PasswordValidator, EmailSender, Logger,….

class UserRepository {
    public User createUser(UserDto user) {
        // INSERT INTO user(...);
    }
}

class PasswordValidator {
    public boolean isStrongPassword(String password) {
        // Matcher.match("Strong password Regex pattern");
    }
}

class EmailSender {
    public void sendEmail(String toEmail, String content) {
        // Get email config
        // Create Gmail object
        // Send email
    }
}

class Logger {
    public void log(String message) {
        // Console.log(....);
    }
}

Last updated