Design Pattern-six principles-dimit's Law

Source: Internet
Author: User

Law of emeter)

Definition: one object should have the least understanding of other objects

The core concept of the dimit rule is inter-class decoupling and weak coupling. Only after weak coupling can the reusability of classes be improved.

 

The image is similar to the following: prisoners in a prison should not be in contact with people outside the prison. Of course, there may be visiting relatives. The prison here is the class, and the prisoner in it is the internal information, and the prison guard in the prison is equivalent to the executor of the Demeter law.

For example

Family visits to prisoners

Family: family members are relatives only with prisoners, but do not know their friends

Package com. loulijun. chapter5;
 
Public class Family {
// Visit prisoners with family members
Public void visitPrisoner (Prisoners prisoners)
{
// The family wants the prisoner and the jailer to help each other
Inmates inmates = prisoners. helpEachOther ();
// Said the jailer. We are allies.
Inmates. weAreFriend ();
}
}

Prisoner: the prisoner and his family are relatives, and the prisoner and his friends are friends.

Package com. loulijun. chapter5;
 
Public class Prisoners {
Private Inmates inmates = new Inmates ();
Public Inmates helpEachOther ()
{
System. out. println ("My family said: you and your friends should help each other ...");
Return inmates;
}
}

Prisoner: the prisoner and the prisoner are friends but do not know his family.

Package com. loulijun. chapter5;
// The meaning of Inmates
Public class Inmates {
Public void weAreFriend ()
{
System. out. println ("the jailer said: we are the jailer ...");
}
}

Scenario: in jail

Package com. loulijun. chapter5;
 
Public class Prison {
Public static void main (String args [])
{
Family family = new Family ();
Family. visitPrisoner (new Prisoners ());
}
}

Running result:

The family said: you and the jailer should help each other...
The jailer said: we are the jailer...

 

Seeing such a result is not awkward. The family told the prisoner to get along with the prisoner, and the prisoner did come to speak. This is clearly out of the border, because the prison only allows the family to visit the prisoners, rather than anyone else can see it.

Communication between our family and our friends is against the rules of dummit, so we need to isolate our family from our friends and reconstruct them.

Family

Package com. loulijun. chapter5;
 
Public class Family {
// Visit prisoners with family members
Public void visitPrisoner (Prisoners prisoners)
{
System. out. print ("said the family :");
Prisoners. helpEachOther ();
}
}

Prisoner

Package com. loulijun. chapter5;
 
Public class Prisoners {
Private Inmates inmates = new Inmates ();
Public Inmates helpEachOther ()
{
System. out. println ("the prisoner and the jailer should help each other ...");
System. out. print ("the prisoner said :");
Inmates. weAreFriend ();
Return inmates;
}

}

Jailer

Package com. loulijun. chapter5;
// The meaning of Inmates
Public class Inmates {
Public void weAreFriend ()
{
System. out. println ("we are friends of hell ...");
}
}

Prison

Package com. loulijun. chapter5;
 
Public class Prison {
Public static void main (String args [])
{
Family family = new Family ();
Family. visitPrisoner (new Prisoners ());
}
}

Running result

The family said: criminals and prisoners should help each other...
The prisoner said: we are prisoners...

 

In this way, the family members and the prisoners are separated, but they also expressed their willingness to help each other. That is, two classes implement information transmission through the third class.

 

There are also the following considerations on the Internet:

① In terms of classification of classes, classes with weak coupling should be created;
② In the structure design of classes, each class should minimize the access permissions of members;
③ In the design of classes, as long as possible, a class should be designed as a constant class;
④ When referencing other classes, the reference of an object to other objects should be minimized;
⑤ Minimize class access permissions;
6. Use the serialization function with caution;
7. Do not expose class members, but provide the corresponding accessors (attributes ).
 


From huarang

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.