Six principles of design pattern: single responsibility principle

There are six principles in design mode: Interface isolation

Six principles of design pattern: dependency inversion principle

Six principles of design pattern: Richter’s substitution principle

There are six principles of design patterns: Demeter’s Law

Six principles of design pattern: open and close principle

Demeter’s Law (LOD) :

Also known as the least knowledge principle. Demeter’s rule is defined as talking only to your immediate friends, not to “strangers.” If two software entities do not have to communicate directly, then direct calls to each other should not occur and the application can be forwarded by a third party. Its purpose is to reduce the degree of coupling between classes and improve the relative independence of modules.

The friend in Demeter’s law refers to the current object itself, the member object of the current object, the object created by the current object, the method parameters of the current object, etc. These objects have association, aggregation or combination relations, and can directly access the methods of these objects.

Advantages:

1. Reduce the coupling degree between classes and improve the relative independence of modules.

2. As the affinity is reduced, the class reusability and system scalability are improved.

Disadvantages:

The excessive use of Demeter’s rule will make the system produce a large number of mediation classes, which will increase the complexity of the system and reduce the communication efficiency between modules. Therefore, the application of Demeter’s rule requires repeated trade-offs to ensure high cohesion and low coupling as well as clear system structure.

To use Demeter’s rule, note:

1. In terms of class division, you should create weakly coupled classes. The weaker the coupling between classes, the better the goal of reuse.

2, in the structure design of the class, as far as possible to reduce the access rights of class members.

3. In class design, give priority to making a class immutable.

4. Minimize the number of references to other objects when referring to other classes.

5. Instead of exposing class attribute members, accessors (set and GET methods) should be provided.

6. Use Serializable with caution.

Source: www.cnblogs.com/az4215/p/11…