6 Great design principles of the Di-Mitt law (Demeter). __ Design mode

Source: Internet
Author: User

The Dimitri Rule (LoD) is also known as the minimum knowledge principle (least knowledge PRINCIPLE,LKP), which, although the name is different, describes the same rule: an object should have a minimal understanding of other objects. In layman's terms, a class should know the least about the classes it needs to be coupled or invoked.

The Dimitri rule has a clear requirement for the low coupling of the class, which contains the following 4 layers of meaning.

1, only communicate with friends

Definition of a friend class: A class that appears in the input and output parameters of a member variable, method is called a member friend class, and a class that appears inside the method body is not a friend class.

Teacher Class

public class Teacher {

The teacher issued an order to the students to clear the girls

public void Commond (Groupleader groupleader) {

Tell the sports commissioner to start the inventory mission.

Groupleader.countgirls ();

}

}

Sports Commissioner Class

public class Groupleader {

Private list<girl> Listgirls;

Pass the class girl in

Public Groupleader (list<girl> _listgirls) {

This.listgirls = _listgirls;

}

inventorying the number of girls

public void Countgirls () {

System.out.println ("The number of Girls is:" + this.listGirls.size ());

}

}

Girls ' class

public class girl{}

Scene class

public class Client {

public static void Main (string[] args) {

Create a group of girls

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

Initializing Girls

for (int i = 0; i < i + +) {

Listgirls.add (New Girl ());

}

Teacher Teacher = new Teacher ();

The teacher issued the order

Teacher.commond (New Grouploader (Listgirls));

}

}

Note: A class only communicates with friends, does not communicate with unfamiliar classes, do not appear geta (). GETB (). GETC (). GETD () This situation (allowing this access in an extreme case, that is, the return type after each point number is the same), the relationship between class and class is built between classes, Rather than between methods, a method tries not to introduce objects that do not exist in a class, except, of course, the classes provided by the JDK APIs.


2, friends are also a distance between

public class Wizard {

Private Random rand = new Random (System.currenttimemillis ());

First step

private int-A () {

System.out.println ("Execute the first method ...");

Return Rand.nextint (100);

}

Second Step

private int second () {

System.out.println ("Execute second method ...");

Return Rand.nextint (100);

}

Third Step

private int third () {

System.out.println ("Execute third method ...");

Return Rand.nextint (100);

}//Software Installation process

public void Installwizard () {

INT-A-this.first ();

See if you need to perform second based on the results returned by the

if (a > 50) {

int second = This.second ();

if (Second > 50) {

int third = this. Third ();

if (Third > 50) {

This.first ();

}

}

}

}

}

Installsoftware class

public class Installsoftware {

public void Installwizard (Wizard Wizard) {

Call directly

Wizard.installwizard ();

}

}

The more public properties or methods that a class exposes, the greater the amount of face that is involved in the modification, and the greater the risk that the change will cause. Therefore, in order to maintain the distance between friends, the design needs to be repeatedly measured: whether it can be reduced to public methods and properties, can be modified to private, package-private (package type, in the class, method, variable before the access rights, the default is the package type), Protected, such as access, can add final keyword and so on.

Note: Dimitri law requires the class "shy" point, as far as possible not to publish too many public methods and non-static variables, as far as possible introverted, more use private, package-private, protected light access.


3, is their own is the

In practical applications there will often be such a method: Put in this class can also be placed in other classes are not wrong, how to measure it. You can stick to the principle that if a method is placed in this class and does not increase the relationship between classes and does not negatively affect the class, it is placed in this class.


4. Use serializable carefully


Best Practices:

The core idea of Dimitri law is decoupling between classes, weak coupling, only weak coupling, the reuse rate of class can be improved. The result of its requirements is to produce a large number of transit or jump classes, resulting in the complexity of the system to improve, but also for the maintenance brought difficulty. The readers need to weigh the Dimitri rules repeatedly, so that the structure is clear and the low coupling is achieved by high cohesion.

In practical applications, if a class jumps more than two times to access another class, it needs to be reconstructed.

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.