What is Dependency Inversion Principle(DIP)
1) High-level modules should not depend on low-level modules. Both should depend on abstractions. 高层模块不应该直接依赖低层模块,两者都应该依赖抽象层。
2) Abstractions should not depend on details. 抽象不能依赖细节,细节必须依赖抽象。
What are “Modules”
Can be varied based on the context:
- From the view of architecture, “modules” can be subsystem
- From the view of subsystem, “modules” can be components
- From the view of components, “modules” can be classes
What are “Dependency”
Can also be varied based on the context:
- High-level modules should not “depend” on low-level modules: high-level modules should not call methods of low-level modules
- High-level modules “depend” on abstraction: high-level modules call methods of abstraction (interface/abstract class)
- Low-level modules “depend” on abstraction: low-level modules implement/extend abstraction (interface/abstract class)