Spis treści
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.
Advantages
- Easier component replacement.
- Easier changes to the software.
- Easier reuse of components.
- Easier testing.
Disadvantages
- The need to use interfaces and abstract classes.
- Labor consumption in creating stable abstractions.
- The need to use factories or dependency injection.
Examples
- 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
- 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
Sources
Main image
Materials
- Clean architecture — Robert C. Martin
- https://sii.pl/blog/solid-dobre-praktyki-programowania/
- https://javadeveloper.pl/solid/
- https://www.p-programowanie.pl/paradygmaty-programowania/zasady-solid/
- https://www.samouczekprogramisty.pl/solid-czyli-dobre-praktyki-w-programowaniu-obiektowym/
- https://pl.wikipedia.org/wiki/SOLID_(programowanie_obiektowe)
- https://en.wikipedia.org/wiki/SOLID
- https://en.wikipedia.org/wiki/Dependency_inversion_principle
- http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
- https://www.samouczekprogramisty.pl/dependency-inversion-principle-dependency-injection-i-inversion-of-control/
- https://programista-doswiadczony.pl/nie-solid-nie-05-dependency-inversion-principle/
- The Dependency Inversion Principle – Robert C. Martin – https://drive.google.com/file/d/0BwhCYaYDn8EgMjdlMWIzNGUtZTQ0NC00ZjQ5LTkwYzQtZjRhMDRlNTQ3ZGMz/view
Sorry, there was a YouTube error.