If the principle of open-end is object-oriented design, then the dependency reversal principle is one of the main implementation mechanisms of object-oriented design, which is the concrete implementation of system abstraction. The dependency reversal principle was Robert c. Martin 's 1996 column for "C++reporter", Engineering notebook's third, which he later added to his 2002 classic "Agile Software development, principles, Patterns, and Practices"in a book. The dependency reversal principle is defined as follows:
Dependency reversal principle (Dependency inversion Principle, DIP): Abstractions should not be dependent on detail, and detail should be dependent on abstraction. In other words, you want to program for the interface, not for the implementation. |
The dependency reversal principle requires us to pass parameters in the program code or in an association relationship, as far as possible to refer to the high level of the abstract layer class, that is, using interfaces and abstract classes for variable type declaration, parameter type declaration, method return type declaration, and data type conversion, and do not use specific classes to do these things. In order to ensure the application of this principle, a concrete class should implement only the methods declared in the interface or abstract class, not the redundant method, otherwise it will not be able to invoke the new method added in the subclass.
After the introduction of the abstraction layer, the system will be very flexible, in the program as far as possible to use the abstract layer of programming, and the specific class written in the configuration file, so that if the system behavior changes, only need to extend the abstraction layer, and modify the configuration file, without modifying the original system source code, The function of the system is extended without modification, which satisfies the requirement of the open and closed principle.
In implementing the dependency reversal principle, we need to program for the abstraction layer and inject the object of the specific class into other objects through dependency injection (dependencyinjection, DI) , which refers to when an object has to be dependent on another object To inject the objects that depend on them through abstraction . There are three common injection methods, namely: construction Injection, value injection (setter injection) and interface injection . Construct injection refers to the object that passes through the constructor to the concrete class, and the value injection refers to the object that passes through the setter method to the concrete class, and interface injection refers to the object of the concrete class through the business method declared in the interface. These methods use abstract types when they are defined, and then pass through the object of a specific type at run time, overriding the parent class object by a subclass object.
|
Extended Software Engineering master Martin Fowler in-depth analysis of dependency injection in its article inversion of Control Containers and the Dependency injection pattern , reference links : Http://martinfowler.com/articles/injection.html |
|
The following is a simple example to deepen the understanding of the dependency reversal principle:
Sunny software Developers in the development of a CRM system found that: the system often needs to store the TXT or Excel files stored in the customer information into the database, so the need for data format conversion. The method of invoking the data Format transformation class in the customer data manipulation class implements the format conversion and the database insert operation, as shown in the initial design scheme structure 1: Figure 1 Schematic diagram of the initial design scheme When coding for the structure shown in Figure 1, the sunny software company developer found a very serious problem with the design, because the data source is not necessarily the same every time the data is transformed, so the data conversion class needs to be replaced. If you sometimes need to change txtdataconvertor to Exceldataconvertor, you need to modify the source code of Customerdao, and you will have to modify the source code of Customerdao when you introduce and use the new Data transformation class. The system is not expansibility, it violates the opening and closing principle, and it needs to reconstruct the scheme now. |
In this example, because Customerdao is programmed for specific data conversion classes, you have to modify the source code of Customerdao when you add a new Data transformation class or replace a data transformation class. We can solve this problem by introducing abstract Data transformation classes, after introducing the abstract Data Transformation class Dataconvertor, Customerdao for the abstract class Dataconvertor programming, and the specific data transformation class name stored in the configuration file, in line with the dependency reversal principle. According to the principle of the substitution of the Richter scale, when the program runs, the specific data conversion class object will replace the Dataconvertor type object, and the program will not have any problems. Change the specific data conversion class without modifying the source code, only need to modify the configuration file, if you need to add a new specific data transformation class, as long as the addition of data transformation class as a subclass of Dataconvertor and modify the configuration file, the original code does not need to make any changes to meet the open and closed principle. The reconstructed structure is shown in 2:
Fig. 2 Structure diagram after reconstruction
In the above reconstruction process, we used the open and close principle, the Richter substitution principle and the dependence reversal principle, in most cases, these three design principles will appear at the same time, the opening and closing principle is the goal, the Richter substitution principle is the foundation, relies on the reversal principle is the means , they complement each other, complement each other, the goal is consistent, Only when analyzing the problem, the angle is different.
Extended Robert C. Martin (Uncle Bob): President of Object Mentor, a senior consultant in the field of OO design, patterns, UML, agile methodologies, and extreme programming. |
|
|
|
Two more Uncle Bob's "Chao",:
Dependency reversal principle of object-oriented design principle