Design principles-LOD Dimit rule

1. Definition

The principle of least knowledge. Each module should only know the limited knowledge of those modules that are closely related to it. In other words, each module only "talks" to its friends, not "talks" to strangers.

There should be no dependencies between classes that should not have direct dependencies; try to rely only on necessary interfaces between classes that have dependencies.

2. Analysis

High cohesion means that similar functions should be placed in the same class, and non-similar functions should not be placed in the same class. Similar functions are often modified at the same time and put into the same class, the modification will be more concentrated, and the code is easy to maintain. The single responsibility principle is a very effective design principle for achieving high code cohesion.

Low coupling means that in the code, the dependencies between classes are simple and clear. Even if two classes have dependencies, code changes in one class will not or rarely cause code changes in dependent classes. Dependency injection, interface isolation, interface-based rather than implementation programming, and Dimit's Law are all to achieve low coupling of code .

3. Case

 

Guess you like

Origin www.cnblogs.com/windpoplar/p/12732874.html