This is the third of five posts talking about the SOLID principles. This principles are a great guide to write good source code. Each post will talk about one of the principles:
- S Single responsability principle
- O Open/Closed principle
- L Liskov substitution principle
- I Interface segregation principle
- D Dependency inversion principle
Liskov substitution principle
Any subtype of a class can be replaced with other subtype of the same class without affecting the correctnes of the software.
One sample could be a way to saparate the database from the rest of the app. Having a good sql interface, if the code works for a particular implementartion (like SQLite), also can work with the implementation for other (like MySQL). The way to do this is to create a base interface that has no method or property directly dependant of an specific implementation.
It’s tempting to use specific functionality of a concrete implementation, because it’s special. But doing so makes the software less reusable.