TOP6. Dependency inversion principle-object-oriented design principle

         Rely on the definition of the inversion principle

The role of the principle of dependence and inversion

Rely on the realization method of the inversion principle


Rely on the definition of the inversion principle


Dependence Inversion Principle (DIP) is  an article published by Robert C. Martin, President of Object Mentor Company, in C++ Report in 1996  .

The original definition of the dependency inversion principle is: high-level modules should not depend on low-level modules, and both should depend on their abstractions; abstraction should not depend on details, and details should depend on abstractions (High level modules should not depend upon low level modules. Both should depend upon abstractions) .Abstractions should not depend upon details. Details should depend upon abstractions). The core idea is: Interface-oriented programming, not implementation-oriented programming.

The principle of dependency inversion is one of the important ways to realize the principle of opening and closing, which reduces the coupling between the client and the implementation module.

In software design, the details are changeable, and the abstraction layer is relatively stable, so the architecture based on abstraction is much more stable than the architecture based on details. The abstract here refers to the interface or abstract class, and the detail refers to the specific implementation class.

The purpose of using interfaces or abstract classes is to formulate specifications and contracts without involving any specific operations, and to delegate the task of revealing details to their implementation classes.

The role of the principle of dependence and inversion


The main functions of the dependency inversion principle are as follows.

  • Relying on the principle of inversion can reduce the coupling between classes.
  • Relying on the inversion principle can improve the stability of the system.
  • Relying on the principle of inversion can reduce the risks caused by parallel development.
  • Relying on the principle of inversion can improve the readability and maintainability of the code.

Rely on the realization method of the inversion principle


The purpose of the dependency inversion principle is to reduce the coupling between classes through interface-oriented programming, so we only need to follow the following 4 points in actual programming to meet this rule in the project.

  1. Each class tries to provide interface or abstract class, or both.
  2. The declaration type of the variable should be an interface or an abstract class as much as possible.
  3. No class should be derived from a concrete class.
  4. Try to follow the Richter substitution principle when using inheritance.


The following uses "customer shopping program" as an example to illustrate the application of the principle of reliance on inversion.

[Example 1] The application of the principle of dependency inversion in the "customer shopping program".

Analysis: This program reflects the relationship between "customer class" and "store class". There is a sell() method in the store class, through which the customer class makes purchases. The following code defines the customer class to shop through ShaoguanShop:

class Customer {
    public void shopping(ShaoguanShop shop) {
        // 购物
        System.out.println(shop.sell());
    }
}

However, this design has disadvantages. If the customer wants to shop from another store (such as WuyuanShop), the customer's code must be modified as follows:

class Customer {
    public void shopping(WuyuanShop shop) {
        // 购物
        System.out.println(shop.sell());
    }
}

Customers have to modify the code every time they change a store, which obviously violates the principle of opening and closing. The reason for the above shortcomings is that the customer class is bound to the specific store class during design, which violates the principle of dependency inversion. The solution is to define the common interface Shop of "Wuyuan Online Store" and "Shaoguan Online Store". The customer class is programmed for this interface, and the code is modified as follows:

class Customer {
    public void shopping(Shop shop) {
        // 购物
        System.out.println(shop.sell());
    }
}

In this way, no matter what store the customer class visits or adds a new store, there is no need to modify the original code. The class diagram is shown in Figure 1.
 

Class diagram of customer shopping program


                                                                      Figure 1 Class diagram of customer shopping program


The program code is as follows:

package principle;

public class DIPtest {
    public static void main(String[] args) {
        Customer wang=new Customer();
        System.out.println("顾客购买以下商品:"); 
        wang.shopping(new ShaoguanShop()); 
        wang.shopping(new WuyuanShop());
    }
}
// 商店
interface Shop {
    public String sell(); //卖
}
// 韶关网店
class ShaoguanShop implements Shop {
    public String sell() {
        return "韶关土特产:香菇、木耳……"; 
    } 
}
// 婺源网店
class WuyuanShop implements Shop {
    public String sell() {
        return "婺源土特产:绿茶、酒糟鱼……"; 
    }
} 
// 顾客
class Customer {
    public void shopping(Shop shop) {
        // 购物
        System.out.println(shop.sell()); 
    }
}

The results of the program are as follows:

顾客购买以下商品:
韶关土特产:香菇、木耳……
婺源土特产:绿茶、酒糟鱼……

<Liskov Substitution Principle                                                                                                                                                           Single Responsibility Principle>

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/106344018