GoF's Demeter law, gof Demeter

Source: Internet
Author: User

GoF's Demeter law, gof Demeter


The cat said to the mouse with a pistol: What is 1 + 1 equal ?. The mouse replied: Wait... 2. Pa ..., When the mouse was killed, the cat blew the barrel and said: you only know too much.

Let's get to know the last principle in this story.

Law Of Demeter)

Definition1: If two classes do not need to communicate directly, they should not interact directly. If one class needs to call a method of another class, a third party needs to forward the call.

Definition2: Minimize the access permissions of members in the structure design of classes.

Advantages:

The fundamental idea of the dimit law is to emphasize the loose coupling between classes. The weaker the coupling between classes, the more conducive it is to reuse. A class with weak coupling is modified, there is no impact on related classes, that is, information hiding promotes the reuse of software.

Since we started to engage with programming, we have learned the general principles of software programming: High Cohesion and low coupling. Both process-oriented and object-oriented programming can improve the code reuse rate only when the coupling between modules is as low as possible. The advantage of low coupling is self-evident, but how can we program to achieve low coupling? That is exactly what the Demeter rule is about to accomplish.

Deepening:

It was first proposed by Ian Holland at Northeastern University in 1987. In general, a class knows little about its dependent classes as well. That is to say, for the dependent class, no matter how complicated the logic is, we try to encapsulate the logic inside the class as much as possible. In addition to the public method provided, we do not disclose any information externally. The dimit rule also has a simpler definition: To communicate with direct friends only. First, let's explain what a friend is: Every object is coupled with other objects. As long as there is a coupling relationship between two objects, we will say that these two objects are friends. There are many coupling methods, such as dependency, association, combination, and aggregation. Here, we call the class in member variables, method parameters, and method return values as a direct friend, while the class in local variables is not a direct friend. That is to say, it is better not to appear in the class as a local variable for a strange class.


Next, the story above: Since the mouse was killed, other mice have summed up a rule -- The dimit rule, in order to ensure a safe life without directly telling the cat what they know, their creed is: the less you know, the better you live.


The above knowledge is relatively basic and easier to learn. The most difficult thing is to apply these principles to program design, because these are all practice.


-------------------------------------- Broken line -----------------------------------------------

The principles are not very familiar yet. We need to continue to explore and practice them, and they are the basis for understanding other models. Therefore, there will be a summary of the six principles, so stay tuned.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.