1: The dependency inversion principle behaves in the Java language:
Dependencies between modules occur through abstraction, and there is no direct dependency between classes, and their dependencies are generated through interfaces or abstract classes.
An interface or abstract class is not dependent on an implementation class.
Implement class dependencies with interfaces or abstract classes.
The dependency inversion principle can reduce the coupling between classes, improve the stability of the system, reduce the risk caused by concurrent exploitation, and improve the readability and maintainability of the code.
Dependencies can be passed on. As long as you do abstract dependencies, even multi-layered dependency transmission is fearless.
Object dependencies are passed in three different ways:
1: Constructors Pass dependent objects
2:setter Method Passing dependent objects
3: interface declares dependent objects
2: Best Practices:
Each class has an interface or abstract class as much as possible, or both.
Interfaces are responsible for defining public properties and methods, and for declaring dependencies on other objects,
Abstract class is responsible for the implementation of the public structure, implementing the class to achieve the exact business logic, while in the appropriate world to the parent class refinement.
The surface type of the variable is as far as the interface or abstract class.
No class should derive from a specific class.
Try not to overwrite (override) The method of the base class.
Combined with the Richter replacement principle.
"Interface-oriented programming" is the core of the dependency inversion principle.
Design Patterns-Design principles-dependency inversion principle