Design Pattern-dependency reversal Principle
The dependency inversion principle is also called the Dependency inversion principle.:
Abstraction should not depend on details. Details should depend on abstraction. To put it bluntly, it is for interface programming, not for implementation programming.
The dependency inversion principle includes three levels of meaning.:
1) high-level modules should not depend on low-level modules, both of which should depend on their abstraction;
2) Abstraction should not depend on details;
3) details should depend on abstraction.
After reading the above explanation, I believe that you will have some questions in your mind like me. Let's talk about them in detail:
1) Why programming for interfaces instead of implementing programming?
In a simple example, the computers we use now have various brands, such as Lenovo, Shenzhou, and Dell,
The mouse and keyboard are required for the computer. If the mouse and keyboard are implemented for a certain brand of machines, what problems will we encounter?
The keyboard and mouse on the market are all different types. One day the mouse or the keyboard is broken, how can we buy it? Do you remember the brand, model, and type of the computer to buy? This will go crazy. Now our computer basically uses the USB interface, whether it is the keyboard or the mouse, we only need to buy the USB interface to use, at the same time, there can be other extensions using the USB interface. As long as this interface is implemented, it doesn't matter how it is implemented. For example, if a small desk lamp with the USB interface is implemented, it can be illuminated as long as it is connected to the USB cable; another example is USB
The interface Charger can be charged after it is connected to our computer.
2) The high-level module should not depend on the lower-level module. Both should depend on its abstraction. How can this problem be understood? You can also ask this question: why is it inverted?
In process-oriented development, in order to reuse Common Code, these commonly used codes are generally written into libraries of many functions. In this way, when we do a new project, you can call these functions.
For example, most of the projects we do need to access the database, so we write the database code into functions and call these functions every time we create a new project, this means that the upper layer depends on the lower layer module.
The problem arises here. When we are working on a new project, we will find that the high-level modules of the business logic are the same. We hope to reuse these high-level modules, however, these high-level modules are bound with the database of the lower-level modules, so we cannot reuse these high-level modules.
If both our high-level and low-level modules depend on abstraction, the specific point is that they depend on interfaces or abstract classes. As long as the interfaces are stable enough, no changes are affected.
3) Why are changes not afraid of dependency on abstract interfaces or abstract classes? To solve this problem, let's take a look at the Lee's replacement principle.
Lee's replacement principle:
If a software object uses a parent class, it must be applicable to its subclass and cannot detect the difference between the parent class object and the subclass object. That is to say, in the software, the parent class is replaced with its subclass, and the program's behavior remains unchanged;
Simply put, child types must be able to replace their parent types. For example, penguins belong to birds in biological classification, but in programming, penguins cannot appear as parent birds.
Suppose there is a parent class of a bird that has a flying method fly (). We cannot let the penguin inherit from the bird, so that when the child class can replace the parent class, when the functions of the software organization are not affected, the parent class can be reused,
Child classes can also add new behaviors based on parent classes. For example, Cats inherit animals and possess behaviors such as eating, drinking, running, and calling as animals,
But one day, we need dogs, cows, and sheep to have similar behaviors, because they all inherit from animals, so apart from changing the instantiation, the program needs to be changed elsewhere.
It is precisely because the child type can be replaced that the module that uses the parent class can be expanded without modification. UML diagram:
A high-level module depends on interfaces or abstract classes. A lower-level module implements interfaces or abstract classes. The principle of dependency inversion is actually that no one depends on whom, except for the agreed interface, so that everyone can be flexible and free.