Introduction to SOLID Design Principles
- SOLID principles are the design principles that enable the user to manage most of the software design problems in the industry both large and small scale.
- The term SOLID is an acronym for five design principles intended to make software designs more understandable, flexible and maintainable.
What does SOLID standards for?
Single Responsibility Principle
Every module or class should have responsibility only over a single part of the functionality provided by the software. A class can have numerous properties and methods where all of these properties and methods lead to a single responsibility.
This principle denoted by Robert C. Martin expresses as “The class should have one and only one reason to change”. which means a class should have only one job.
The benefit of this principle is smaller classes are easier to read, maintain and unit test, lower coupling and microservices organization.
The principle states that“Software entities (classes, modules, functions, etc.) should be open for extension but closed for modifications”.
This means the design and writing of the code should be done in a way that coding new functionality (Open For Extension)should be able to integrate without changing the existing code(Closed for Modification).
Implementing class as an abstract class and implement methods as abstract methods, it promotes the use of interfaces to enable you to adapt the functionality of your application without changing the existing code. This enables loose coupling.
Liskov Substitution Principle
This principle is an extension of the OCP which was introduced by Barbara Liskov in 1987 during her conference address on Data abstraction and hierarchy. She states the objects of a superclass shall be replaceable with objects of its subclasses this means derived types must be completely substitutable for their base types.
Example: if S is a subtype of T, then objects of type T may be replaced with objects of type S.
It requires the objects of the subclass(child class) to behave in the same way as the objects of its superclass(parent class) because of the inheritance concept in Object-oriented programming.
Interface Segregation Principle
“Many client-specific interfaces are better than one general-purpose interface”.We should not enforce clients to implement upon interfaces that they don’t use.
Easier to violate the concept, especially when the software grows eventually when the new requirements arise and have to add more and more features. Instead of creating one larger interface we can break down it to smaller interfaces to ensure that implementing classes only need to be concerned about the methods with each one serving one submodule.
Dependency Inversion Principle
This principle denotes that “High-level modules should not depend on low-level modules. Both should depend on abstractions”. The principle also states that abstractions should not depend on details. Details should depend on abstractions.
The main purpose of this principle is to reduce coupling, this can be achieved by following both the Open Closed Principle and the Liskov Substitution Principle.
Benefits SOLID Principles helps us to
- Achieve a reduction in the complexity of code.
- Increase readability, extensibility, and maintenance.
- Reduce error and implement Reusability.
- Achieve Better testability
- Reduce tight coupling.