SOLID Principle
Last updated
Last updated
The SOLID principles are an object-oriented approach applied to software structure design. It was introduced by Robert C. Martin.
The design principles encourage us to create more maintainable, understandable, and flexible software. Consequently, as our applications grow in size, we can reduce their complexity.
The word SOLID is an acronym for:
SOLID principles complement each other and work together to achieve the common purpose of well-designed software.
To understand this principle, let's see an example with a class that violates this principle as follows:
Violate SRP: The PartnerWebClientImpl class does many things: send to partner 1, partner 2, and a default partner.
Violate OCP: If you have one more Partner, you have to change all classes.
Violate ICP: The implementation of sendToDefaultPartner() method alters the correctness of the partner. It should do something or remove completely.
Violate ISP: One general-purpose interface PartnerWebClient.
Violate DIP: The OrderService depends on the implementation PartnerWebClientImpl. It should depend on the interface PartnerWebClient.
Let's see how to apply SOLID:
Follow SRP: each class does one thing.
Follow OCP: easy to add new partners without modifying any existing class.
Follow ICP: default partner doesn't change the correctness of a partner.
Follow ISP: no general purpose or large interfaces.
Follow DIP: class depends on interfaces, not depends on implementation.