Design Pattern Principle 4: dumit's Law

Source: Internet
Author: User

Law of Demeter (dump) is also known as the least knowledge principle (LKP ).

Problem: The closer the relationship between classes, the greater the coupling degree. When a class changes, the greater the impact on the other class.

An object should have a minimum understanding of other objects. In layman's terms, a class should know the least about the classes that need to be coupled or called. I have nothing to do with how complicated your (coupled or called class) is internally, that's your problem. I knew the public method you provided, and I called so much, and I didn't care about anything else.

Meaning: only communicate with friends. The definition of friend class is as follows: the class that appears in the input and output parameters of member variables and methods is called the member friend class, the class that appears in the method body does not belong to the friend class.

The followingCodeOther classes are dependent on the method body, which seriously violates the Demeter rule.

Public class teacher {

Public void commond (groupleader ){

List <girl> listgirls = new arraylist <girl> ();



For (INT I = 0; I <20; I ++ ){

Listgirls. Add (New Girl ());

}

Groupleader. countgirls (listgirls );

}

}

A method is an action of a class, and the class does not know that its behavior is dependent on other classes. This is not allowed.

The correct method is:

Public class teacher {

Public void commond (groupleader ){

Groupleader. countgirls ();

}

}

Public class groupleader {

Private list <girl> listgirls;



Public groupleader (list <girl> _ listgirls ){

This. listgirls = _ listgirls;

}



Public void countgirls (){

System. Out. println ("the number of girls is:" + listgirls. Size ());

}

}

Note: A class only communicates with friends and does not communicate with strangers. Do not use geta (). getb (). GETC (). getd () (in an extreme case, this access is allowed, that is, the return types after each vertex are the same ), the relationship between classes is established between classes, rather than between methods. Therefore, a method should try not to introduce objects that do not exist in a class, except for classes provided by JDK APIs.

There is a distance between friends.

The more public attributes or methods a class exposes, the larger the scope involved in the modification, and the larger the risk spread caused by the change. Therefore, in order to keep the distance between friend classes, we need to repeatedly measure whether the public method and attribute can be reduced, and whether the attribute can be changed to private or package-private (package type, if no access permission is added before a class, method, or variable, the package type is used by default.) Access Permissions such as protected and final keywords can be added.

Note: The dimit rule requires the class to be "Shy". Do not publish too many public methods and non-static public variables as much as possible, so as to be restrained as much as possible, use private, package-private, protected, and other access permissions.


It is your own: if a method is put in this class, neither adding inter-class relationships nor negatively affecting this class, it will be placed in this class.

Use serializable with caution

Finally, the core concept of the Demeter law is inter-class decoupling and weak coupling. Only after weak coupling can the Reuse Rate of classes be improved.

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.