An explanation of dependency inversion principle--seven object-oriented design principles (3)

Source: Internet
Author: User

Dependency Inversion principle Source:

          a Direct dependent class b Span style= "Color:rgb (75,75,75)" If you want the class a c , you must modify the class a a b c a

dependency Inversion principle ( dependence Inversion Principle ) is that the program relies on an abstract interface and does not rely on a specific implementation. Simply put, it requires programming the abstraction, not programming the implementation, which reduces the coupling between the customer and the implementation module.

A. high-level modules should not rely on lower-layer modules, both should rely on their abstraction. abstractions should not depend on detail, and detail should be dependent on abstraction. (Detail: A specific implementation class that implements an interface or inherits classes generated by an abstract class that can be instantiated directly by the keyword new .) )

B. An abstract class or interface that cannot be instantiated. .

Dependency inversion principle point of departure: abstract things are much more stable than the variability of details. Structures built on the basis of abstraction are much more stable than structures built on the basis of detail. by abstracting (abstract classes or interfaces), the implementations of each class or module are independent of each other and do not affect each other, allowing for loose coupling between modules. The purpose of using interfaces or abstract classes is to make good specifications and contracts without involving any specific operations, and to give the task of presenting details to their implementation class to complete. This is also the basis of the open and closed principle.

three ways to implement dependency inversion principle :

(1). passing a dependent object through a constructor function;

For example, the arguments that need to be passed in the constructor are implemented in the form of an abstract class or interface.

(2). through Setter method to pass dependent objects;

that is, when we set the Set the parameter in the method is an abstract class or interface that implements the transitive dependent object.

(3). the interface declaration implements the dependent object, also called the interface injection;

That is, in the function declaration, the argument is an abstract class or interface, which implements the transitive dependent object, thus achieving the purpose of directly using the dependent object.

can be summarized as interface passing, construction method passing and Setter method is passed. The core of the dependency inversion principle is that we should interface programming, understand interface-oriented programming, and understand dependency inversion;

Examples of dependency inversion principles:

In the development of our financial system, we often need to transfer K-line data or tick data to other files or reports, so we need to convert the data format. 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:


When coding the structure shown in Figure 1, we found a very serious problem with this design, because the data source is not necessarily the same every time the data is converted, so you need to change the data conversion class, such as sometimes need to change the klinedataconvertor to Mindataconvertor, At this point, you need to modify the source code of the Customerdao, and in the introduction and use of the new Data transformation class also have to modify the source code of Customerdao, the system is poor scalability, violation of the open and close principle, now need to reconstruct the scheme.

Since Customerdao is programmed for specific data conversion classes, it is necessary to modify the source code of Customerdao when adding new data transformation classes or replacing data transformation classes. 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 concrete data transformation class, as long as the addition of data transformation class as a subclass of Dataconvertor and modify the download file structure, the original code does not need to make any changes to meet the open and closed principle. The reconstructed structure is shown in 2:


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.

Dependency Inversion principle Use summary:

(1). Low-level modules should have an abstract class or interface as much as possible, or both.

(2). The declaration type of a variable should be an abstract class or interface as much as possible.

(3). Follow the Richter substitution principle when using inheritance.

An explanation of dependency inversion principle--seven object-oriented design principles (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.