DEV Community

Cover image for SOLID in Software Development - 1
ajay-8192
ajay-8192

Posted on

SOLID in Software Development - 1

What is meant by SOLID?

SOLID in Software Development is 5 principles that are a set of guidelines for designing and developing software systems that are object-oriented, reusable, maintainable, and extensible.

  1. Single Responsibility Principle (SRP)
  2. Open-Closed Principle (OCP)
  3. Liskov Substitution Principle (LSP)
  4. Interface Segregation Principle (ISP)
  5. Dependency Inversion Principle (DIP)

Why SOLID?

Some of the benefits of following the SOLID principles:

  • Reduced code complexity: Helps to reduce the complexity of code by making it more modular and easier to understand. This makes it easier for developers to read, maintain, and modify the code.

  • Increased code reusability: Makes it easier to reuse code by making it more modular and independent of other parts of the codebase. This can save developers time and effort, and it can lead to more consistent and reliable code.

  • Improved code maintainability: Makes it easier to maintain code by making it more modular and easier to test. This can save developers time and effort, and it can help to prevent bugs from being introduced into the code.

  • Enhanced code extensibility: Makes it easier to extend code by making it more modular and easier to change. This allows developers to add new functionality to the code without breaking existing code.

Single Responsibility Principle (SRP)

The Single Responsibility Principle (SRP) is a class that should be responsible for a single functionality and should not be burdened with multiple responsibilities that are unrelated to its core purpose.

Here is an example of how the SRP can be violated:

public class Customer {
    private String name;
    private String email;
    private String address;
    private String phoneNumber;

    public void updateCustomerDetails(String name, String email, String address, String phoneNumber)

{
        this.name = name;
        this.email = email;
        this.address = address;
        this.phoneNumber = phoneNumber;
    }

    public void sendPasswordResetEmail() {
        // Send password reset email to the customer's email address
    }
}
Enter fullscreen mode Exit fullscreen mode

This Customer class has two responsibilities: managing customer details and sending password reset emails. If the way that password reset emails are sent changes, then the updateCustomerDetails method will need to be changed as well. This violates the SRP because the updateCustomerDetails method should only be responsible for updating customer details.

A better way to design this code would be to split the Customer class into two classes: one for managing customer details and one for sending password reset emails:

public class CustomerDetails {
    private String name;
    private String email;
    private String address;
    private String phoneNumber;

    public void updateCustomerDetails(String name, String email, String address, String phoneNumber)

{
        this.name = name;
        this.email = email;
        this.address = address;
        this.phoneNumber = phoneNumber;
    }
}

public class PasswordResetService {
    private CustomerDetails customerDetails;

    public PasswordResetService(CustomerDetails customerDetails) {
        this.customerDetails = customerDetails;
    }

    public void sendPasswordResetEmail() {
        // Send password reset email to the customer's email address
    }
}
Enter fullscreen mode Exit fullscreen mode

This code follows the SRP because the CustomerDetails class is only responsible for managing customer details, and the PasswordResetService class is only responsible for sending password reset emails. If the way that password reset emails are sent changes, then only the PasswordResetService class will need to be changed.

***** More to Part 2 *****
**** 💻ALL THE BEST💻 ****

Top comments (0)