Analysis and Explanation of java Object Oriented Design Principle Based on Dimitri's Law

  • 2021-11-29 06:50:45
  • OfStack

Expansion of the use of directory concept

Concept

Dimitri's law solves the problem of coupling degree between classes. If class A calls a method of class B, the two classes form a tight coupling mode. When this method of class B changes, 1 will definitely affect the execution result of class A. Dimitri's Law requires that every class has as little relationship with other classes as possible, that is, when other classes change as much as possible, the impact on the execution results of their codes will be minimized.

Typical case: A class calls the method of B class, and B class and C class are a kind of association relationship. If A class directly calls the method of C class through C class object held by B class, A class and C class have strong coupling relationship at the same time. The code is as follows:


public class B {
    public C c = new C();
}
public class C {
    public void fun()
    {
        // Related code 
    }
}
public class A {
    public void show(){
        B b = new B();
        b.c.fun();
    }
}

There is a strong coupling between A and C. When the code of fun in C changes, 1 will definitely affect A, which does not conform to Dimitri's law. According to the requirements of Dimitri's Law, it can be modified to:


public class B {
    private C c = new C();
    public void fun(){
       c.fun(); 
    }
}
public class A {
    public void show(){
        B b = new B();
        b.fun();
    }
}

A and C codes are completely decoupled. When fun code of C changes, only fun code in class B needs to be modified; When the business logic in A changes, the code in fun in B needs to be modified, and only the code in B needs to be modified, which has nothing to do with the code in C.
Dimitri's law is also called the least knowledge principle (LKP), which means that an object should know as little as possible about other objects. Generally speaking, "don't talk to strangers, only communicate with friends". In the above example, classes A and B are friends, and classes C are strangers.

Use

Dimitri's rule solves the coupling problem between classes, which makes the interface communication between classes simple and improves the maintainability, but it also increases the calling level and complexity. However, the following situation 1 must use Dimitri's rule to avoid risks.

1. When using third-party components or controls, add a wrapper class to make the caller and third-party components completely decoupled. For example, XP style1 group controls used in desktop programs belong to third-party controls, and each control adds a wrapper class. Whenever XP style controls cannot be used, such as free sudden change charges, we only need to modify the code in the wrapper class.

2. For the modules that are expected to change greatly, an appearance layer is added to simplify and stabilize the calling relationship of the high-level modules, and decouple from the unstable parts.

3. To deal with new team members or programmers with poor code quality, add an appearance layer to avoid frequent modifications, resulting in the whole program hanging up.

Expand

1. It means to reduce the coupling between objects and improve the maintainability of the system;

2. Dimitri's rule is a design idea, which is not only embodied in the coupling degree between objects, but widely applied to the relationship between layers in various hierarchical structures. Each layer forms an isolation relationship, and it is not necessary to know the calling relationship within the layer and the next level. For example, MVP mode, P combines M and V, so that M and V can be changed independently, and any change in one side only affects the change of P layer code.

3. The use of independent API in front-end development is the requirement of Dimitri's law, such as:


 var api = {
   adminLogin: (param) => post(apiBase+"/sys/login", param),
   userLogin: (param) => post(apiBase+"/sys/weblogin", param),
   }

When calling a page, you only need to call the adminLogin method defined in api. When the road name apiBase and method name login defined in the back end change, you can only modify api without affecting the code of the calling page
4. Object adapter, proxy pattern and facade pattern in design pattern all embody the idea of Dimitri's rule.

Above is java Object-Oriented Design Principle of Dimitral Law Analysis Detailed Explanation Detailed Content, more information about java Object-Oriented Design Principle Please pay attention to other related articles on this site!


Related articles: