61.25 SOLID principles


Why use SOLID principles?

Let’s look at the issues below that may occur in the code if we don’t adhere to the SOLID principles.

However, if we adhere to the SOLID principles, we can do the following:

SRP (Single Responsibility Principle)

"A class should have only one reason to change."
- Robert C. Martin

When a class performs one task, it contains a small number of methods and member variables that are self-explanatory. SRP achieves this goal, and due to this, our classes are more usable, and they provide easier maintenance.

OCP (Open/Closed Principle)

"A software artifact should be open for extension but closed for modification."
- Bertrand Meyer

Add new code to improve quickly instead of changing the core code, which should be kept unique and reusable.

OCP can be implemented through inheritance or interfaces. The latter, also known as polymorphic OCP, allows for extension while keeping the original code closed for modification.

Liskov Substitution Principle (LSP)

Liskov Substitution Principle (LSP) states that the objects of a subclass should behave the same way as the objects of the superclass, such that they are replaceable. This rule generally applies to abstraction concepts like inheritance and polymorphism.

Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) is a design principle that does not recommend having methods that an interface would not use and require. Therefore, it goes against having fat interfaces in classes and prefers having small interfaces with a group of methods, each serving a particular purpose.

Dependency Inversion Principle (DIP)

The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules, but rather both should depend on abstractions. The abstractions should not depend on details. Instead, the details should depend on abstractions.

Thoughts 🤔 by Soumendra Kumar Sahoo is licensed under CC BY 4.0