Object-Oriented Design Principle (V)-Dependency inversion principle

Source: Internet
Author: User

The "open-closed" principle is our goal of OOD. The main mechanism to achieve this goal is"Dependency reversal"Principle.

What is the principle of dependency inversion??

(DIP) the content of this principle is: to rely on abstraction, not on specifics. Or: You need to program the interface, rather than implement programming.
For an abstract level, it is the generalization of the essence of a system. It is the business logic of the system and the macro, strategic decision, and the embodiment of inevitability; the specific layer is the algorithm and logic related to the implementation. Tactical decisions come with considerable contingency. Traditional procedural system design methods tend to make high-level modules depend on lower-level modules, and abstract layers depend on specific layers. This is actually the macro-level decision, the tactical decision strategy, and the occasional decision inevitable. The principle of dependency inversion is to reverse the dependency of such errors.

Dependency inversion principle:

Many construction design models, such as COM, CORBA, JavaBean, and EJB, follow the basic principle of DIP.

For the two goals of software design, reuse and maintainability, the traditional design focuses on the reuse and maintenance of specific modules, such as algorithms, data structures, and function libraries. However, the abstraction of the system is relatively stable, and its reuse is very important. At the same time, the maintainability of the abstraction layer should also be a key point. That is to say, DIP also leads to a "reversal" of reuse and maintainability"

Let's take a look at the dependency types, which are coupling. There are three types:
----- Nil Coupling relationship. The two classes have no dependency, that is, zero Coupling.
----- A Concrete Coupling relationship. Two classes have a dependency relationship, which means a specific Coupling relationship. If a specific class directly references another specific class, this relationship will occur.
----- Abstract Coupling relationship occurs between a specific class and an Abstract class, which maximizes flexibility between classes that must have a relationship.
Dip requires the client to rely on abstract coupling. abstraction should not depend on details, and details should depend on abstraction (abstractions shocould not depend upon details. details shoshould depend upon implements actions), another expression of this principle is the one emphasized by the "four-person group": To program interfaces, do not implement programming (program to an interface, not an implementation), when the program needs to reference an object, it should try to use the abstract type as the static type of the variable, which is the meaning of interface programming. DIP is a way to achieve the "open-closed" principle.
To achieve dip, abstract coupling is the key. Because an abstract coupling always involves the inheritance of a specific class from an abstract class. It is also necessary to ensure that the sub-classes can be changed to any reference to a certain type. Therefore, LSP is the basis of dip. DIP is the core principle of OOD. The research and application of design patterns use it as the guiding principle. Although dip is powerful, it is difficult to implement it. In addition, dip assumes that all specific classes will change, which is not all right. Some specific classes are quite stable. The client that uses this class can depend entirely on this specific class without having to create another abstract class. (Reference ends here)
For the functions of interfaces and interfaces, we can use an analogy to understand them. This will be more intuitive. The sockets we usually use can be seen as an interface, we can only use it to use electricity. If the power supply method changes, for example, from current thermal power generation to solar power generation, the entire power supply line should be comprehensively modified, then, our electricity usage method will not be affected, or we will use electricity through sockets. Changes in Power Supply Methods and line changes are transparent to us, the benefits of this operation can be seen by comparing the following situations. If we do not use an electrical outlet to use electricity, we can directly connect to the power supply station through a wire. We will not talk about the security issues, if the power supply method changes and the line needs to be modified, someone may knock on your door and say, "Hello, we're here to modify the wire ", and it may be at a.m.. Why? Every household in the city needs such modification. The workload of circuit modification personnel is too large, and only of work is required. So open the door, or you will have to live without electricity. So the benefits we bring through the socket (Interface) are obvious? It reduces the coupling between the electricity supplier and the power supplier and facilitates the implementation of changes.
Interfaces are the key to implementing pluggability. The connection between objects does not have to be hard bound (hardwire) to a specific class object, instead, it is bound to a high-level abstraction (interface or abstract class), which increases the flexibility of the class and allows you to freely perform "plugging ". Interfaces or abstract classes can reduce coupling between classes. An object can be easily replaced by another object that implements the same interface without affecting the object itself, therefore, all changes are made without the knowledge of this object.

A system provides a data conversion module that converts data from different data sources into multiple formats. For example, you can convert data from databases) you can also convert the data (textsource) from a text file. The converted format can be XML file (xmltransformer) or xls file (xlstransformer


Figure (1) Why do we go to figure (2? Because the system may need to add a new data source or file format. Each time a new data source or file format is added, the source code of the customer MainClass needs to be modified, in order to use the new class, but violates the principle of open and closed. The dependency inversion principle is used to reconstruct the dependency.

The high-level module should not depend on the underlying module, both of which should be dependent on and abstract; abstraction should not depend on details, and details should depend on abstraction.

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.