S.O.L.I.D. STANDS FOR:
- S — Single responsibility principle
- O — Open closed principle
- L — Liskov substitution principle
- I — Interface segregation principle
- D — Dependency Inversion principle
Examples taken from:
- https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design
- https://dzone.com/articles/the-solid-principles-in-real-life
Single responsibility principle
"Just because you can, doesn’t mean you should."
- A class should have one and only one reason to change, meaning that a class should have only one job.
- Avoid GOD classes
- Class should be small
- Split big classes into smaller classes
Open/Closed principle
- Classes should be open for extension but closed for moditication
- This simply means that a class should be easily extendable without modifying the class itself
- Use private variables with getters/setters
- Use abstract base class
Liskov substitution principle
"If it looks like a duck, quacks like a duck, but needs batteries – you probably have the wrong abstraction."
- Objects in a program would be replacable with instances of their subtypes without altering correctness of program
- Violations will often fails "is a" test
- A square is a rectangle
- A rectangle is not a square
Interface Segregation principle
- Make fine grained interfaces that are client specific
- Many client specific interfaces are better than a general purpose interface
- Keep your components focused and minimise dependencies between them
Dependency Inversion principle
- Abstractions should not depend upon details
- Details should not depend upon abstractions
- Important that higher level and lower level objects depends on the same abstract interaction
- They should not share the same dependency by keeping them independent but by having a clearly API between them.