SOLID Principles
- SOLID is an acronym for 5 important design principles when doing OOP
- Single Responsibility, Open-closed, Liskov Substitution, Interface Segregation and Dependency Inversion.
- Introduced by Robert C. Martin (Uncle Bob). Design Principles and Design Patterns
- Intention of SOLID is to make software designs more understandable, easier to maintain and easier to extend.
Single Responsibility Principle
One class should only serve one purpose. Do not mix (save() and get() in same function)
Open-closed Principle
Entities should be open for extension, but closed for modification. (Polymorphism)
Liskov Substitution Principle
“Data abstraction”, about bad inheritance
-
Tech explain:
Let φ(x) be a property provable about objects x of type T. Then φ(y) should be true for objects y of type S where S is a subtype of T.
-
Human explain:
- Preconditions cannot be strengthened in a subtype.
- Postconditions cannot be weakened in a subtype.
- Invariants of the supertype must be preserved in a subtype.
-
Robert Martin: Subclass/derived class should be substitutable for their base/parent class.
Interface Segregation Principle
A Client should not be forced to implement an interface that it doesn’t use. Break our interfaces in many smaller ones
Dependency Inversion Principle
High-level modules should not depend on low-level modules. Both should depend on abstractions. (About decoupling by share abstraction)
Abstractions should not depend on details. Details should depend on abstractions.
Ref: