The Dimitri rule (law of Demeter) is also called the least knowledge principle (Least knowledge Principle shorthand LKP), which means that an object should have as little understanding of other objects as possible and not speak to strangers. The English shorthand is: LoD.
The Dimitri law can be simply said:
Talk only to your immediate friends. For Ood, it is also explained in the following ways: a software entity should interact with as few interactions as possible with other entities. Each software unit has only minimal knowledge of the other units and is limited to the software units that are closely related to the unit. The purpose of the Dimitri law is to reduce the coupling between classes. Because each class minimizes its reliance on other classes, very easy makes the function modules of the system independent and does not exist (or very rarely) dependent on one another. The Dimitri law does not want the class directly to establish direct contact.
Suppose there really is a need to establish a connection, and hopefully it can be conveyed through its friend class. Therefore, one of the possible consequences of applying the Dimitri law is that there are a large number of intermediary classes in the system that exist entirely to convey the mutual invocation relationship between classes-which adds to some degree the complexity of the system. Interested in being able to study the design pattern of the façade mode (facade) and the mediation Mode (mediator). Are all examples of the application of the Dimitri Law. It is worth mentioning that although Ian Holland's contribution to computer science is limited to this rule. Other aspects of the achievements are not many, but the law is not confined to the computer field, in other areas are the same.
Example. Americans use this law in the design of space systems.
The narrow Dimitri rule assumes that two classes do not have to communicate directly with each other, then these two classes should not have a direct interaction.
Suppose one of the classes needs to invoke one of the methods of a class and can forward the call through a third party.
A friend's circle determines the "friend" condition: 1) The current object itself (this) 2) is passed in as a reference to the object in the current object Method 3) object directly referenced by an instance variable of the current object 4) The instance variable of the current object is assumed to be an aggregation, So the elements in the gathering are all friends. 5) objects created by the current object regardless of an object, assume that one of the above conditions is satisfied. is the "friend" of the current object, or "stranger". The disadvantage of the narrow Dimitri Law is that there are a lot of small methods in the system. These methods, however, pass indirect calls regardless of the business logic of the system. Following the Dimitri rule between classes will be a simplification of the local design of a system. Since each part is not directly associated with objects of long distance. However, this also causes the communication efficiency between different modules of the system to be reduced, but also makes the system not easy to coordinate between the different modules. Façade mode and mediator mode are actually the application of the Dimitri Law. The generalized principle of Dimitri is embodied in the design of classes: It is preferable to set a class to the invariant class.
Minimize access to one class. Use serializable carefully. Minimize entry to members.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Design mode 6 major principles (5)-The Artemis