Design Pattern Learning: Dimitri Law

Source: Internet
Author: User

The Dimitri rule (law Ofdemeter,lod), also known as the minimum knowledge principle (least knowledge PRINCIPLE,LKP), is defined as: an object that understands as little as possible to other objects.

There are many popular sayings, for example, only communicate with your direct friends; don't talk to "strangers";

This is done to reduce the coupling between classes. Think about the time of class, the teacher often said cohesion well-structured classes poly, low coupling. In order to better understand, we say some digression. Take a notebook computer for example, remember when I was a child, I tore down the notebook computer, the internal parts of the same as the current notebook. The connection between the parts will be more, unlike the current notebook, said that the demolition can be removed quickly. What is this for? Because now the notebook than my childhood notebook, in the internal components, more independent, a component of the highly cohesive components of their own functional responsibilities, external to provide a simple interface. I think if you do not, then when we want to tear down a notebook, it will be very troublesome, the internal data wire winding, you can imagine the scene, even a skilled technical workers, may not be able to quickly remove the notebook. But after a high level of cohesion, we can easily be removed. And, the benefit of doing this is not only that, when the notebook is broken, we can also be faster testing the part of the broken down, and then remove the part, put the new parts can be used immediately. These are the benefits of high cohesion, low coupling.

Hardware so, software is the same, with similarities, there are many reasons for us to ensure that the product internal high cohesion, low coupling.

Complying with the Dimitri law allows our design to reduce the coupling between classes. For a better understanding, let's start with an example.

Most people should have seen the "Godfather" trilogy (not seen), the Godfather's name is related to the church, and indeed, the godfather of the film believes in Christianity, but he is not a Christian post. Godfather is actually a Triad boss to himself to obtain a good pronoun just, because this triad boss than the general Triad boss higher on several grades. Now the Godfather wants to kill (because someone is next to him). Look at the following paragraph that does not meet the Dimitri rules of design and code.

Code:

Class person{public
         String name;
Class godfather{
         coremember coremember;
         public void Kill (person someone)
         {
                   Killer killer = new Killer ();
                   Killer.kill (someone);
         }
 
Class coremember{
         private Killer Killer;
}
 
Class killer{public
         void Kill (Person someone)
         {
                   System.out.println (someone.name+ "killed");
         }

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.