☆The principle of dependency inversion depends on abstraction rather than specific implementation.
8.3 dependency reversal Principle
☆Three Coupling Relationships
① Zero coupling relationship. If there is no coupling relationship between the two classes, it is called zero coupling;
② Specific coupling occurs between two specific classes, caused by direct reference of another specific class by one class.
③ Abstract Coupling Relationships: Abstract coupling relationships occur between a specific class and an abstract class. There is maximum flexibility between two classes that must have a relationship.
☆Another statement of the dependency reversal principle is:
Do not program the interface (program to an interface, not an implementation) [gof95]. Similarly, when dealing with Coupling Relationships between classes, try to use abstract coupling forms.
A and B on the left are specific dependencies. After reconstruction, it becomes abstract dependency, as shown in the right figure:
☆The lean replacement principle is the basis of the dependency reversal principle.
☆The factory model, template model, and iteration submodel are the embodiment of the dependency reversal principle.
8.5 Java's support for abstract types
☆In design, the commonly used abstraction method is: first abstract something logically fixed, define an interface, and then define an abstract class to implement this interface, abstract classes implemented by this interface by default should implement most of the publicCodeThe specific class is generally derived from this abstract class.
Examples of containers in Java are as follows: