Dependency inversion principle
High-level modules should not depend on low-level modules, but the dependence should result from abstraction.
All high level modules should be connected with low-level modules by abstraction. Abstractions should not depend on details, but details should depend on abstraction.
- Easier component replacement.
- Easier changes to the software.
- Easier reuse of components.
- Easier testing.
- The need to use interfaces and abstract classes.
- Labor consumption in creating stable abstractions.
- The need to use factories or dependency injection.
- The report generation class (high level) uses the class that logs information about exceptions in the application (low level). He must convey, among others path to the log folder. Changing the class logging into the file to the class logging into the database will force a change in the class generating the report because you will have to pass other data to the constructor of the logging class.
Copier class using the Reader class and the Writer class. If we have used classes, and we want to change the way of saving (from printing to a printer, to generating pdf), we will also have to change the Copier class or create a new class Copier PDF. If the Copier class had references to the IWrite interfaces, then it would be enough to inject an appropriate implementation of the IWriter to achieve our goal.
All posts from mini project: Learn SOLID and OOP principles:
- SOLID exercises
- S like Single responsibility principle
- SOLID exercises — Kata
- O as Open-closed principle
- L jak Liskov Substitution Principle
- Interface segregation principle
- KISS — Keep it simple, stupid
- DRY — Don’t repeat yourself
- Dependency inversion principle
- SLAP — Single Level of Abstraction Principle
- Composition Over Inheritance
- Encapsulate what changes
- Lod — Law of Demeter
- ES as Exercises of Single responsibility principle
- EO as Exercises of Open/closed principle
- EL as Exercises of Liskov Substitution Principle
- EI as Eexrcises of Interface segregation principle
- ES as Exercises of Dependency Inversion Principle
- Object-oriented programming
- OOP — Object-Oriented Programming — Advice
- OOP — Object Oriented Programming
- Clean architecture — Robert C. Martin
- The Dependency Inversion Principle – Robert C. Martin – https://drive.google.com/file/d/0BwhCYaYDn8EgMjdlMWIzNGUtZTQ0NC00ZjQ5LTkwYzQtZjRhMDRlNTQ3ZGMz/view