Demeter’s rule

The Law of Demeter (LoD) is also called the Least Knowledge Principe (LKP).

Demeter’s rule is defined as: Talk only to your immediate friends, not to “strangers.” The implication is that if two software entities do not communicate directly, then direct calls to each other should not occur and can be forwarded by a third party. Its purpose is to reduce the degree of coupling between classes and improve the relative independence of modules.

The “friend” in Demeter’s law refers to: the current object itself, the current object’s member objects, the current object created by the current object, method parameters of the current object, etc.


Demeter’s rule

Demeter’s law requires to limit the width and depth of communication between software entities. The correct use of Demeter’s law will have the following two advantages:

  1. The degree of coupling between classes is reduced and the relative independence of modules is provided.
  2. As the affinity is reduced, the class reusability and system scalability are improved.


However, the excessive use of Demeter’s rule will make the system produce a large number of mediation classes, which will increase the complexity of the system and reduce the communication efficiency between modules. Therefore, the use of Demeter’s law requires a trade-off of fattening to ensure high cohesion and low coupling while ensuring a clear structure of the system.


The realization method of Demeter’s rule

  1. From a dependent’s point of view, only rely on what should be relied on.
  2. From the point of view of the dependent, only expose what should be exposed.


So here are six things to keep in mind when applying Demeter’s rule:

  1. In class partitioning, you should create weakly coupled classes. The weaker the coupling between classes, the better the goal of reuse.
  2. In the structure design of the class, the access rights of class members should be reduced as far as possible.
  3. In class design, the priority is to make a class immutable.
  4. Keep references to other objects to a minimum on references to other classes.
  5. Instead of exposing the class’s attribute members, the corresponding access rights should be provided (set and GET methods)
  6. Use Serialable with caution.


An example of the relationship between a star and his agent.



Analysis: Celebrities devote themselves to their art, so many daily affairs are handled by their agents, such as meeting with fans and negotiating with media companies. Here, agents are friends of stars, while fans and media companies are strangers, so it is appropriate to apply Demeter’s rule, whose class diagram is shown in Figure 1.


The program code is as follows:

package principle;
public class LoDtest
{
    public static void main(String[] args)
    {
        Agent agent=new Agent();
        agent.setStar(new Star("Ruby"));
        agent.setFans(new Fans("Fan Han Seung"));
        agent.setCompany(new Company("China Media Limited")); agent.meeting(); agent.business(); } // class Agent {private Star myStar; private Fans myFans; private Company myCompany; public voidsetStar(Star myStar)
    {
        this.myStar=myStar;
    }
    public void setFans(Fans myFans)
    {
        this.myFans=myFans;
    }
    public void setCompany(Company myCompany)
    {
        this.myCompany=myCompany;
    }
    public void meeting()
    {
        System.out.println(myFans.getName()+"With the Stars"+myStar.getName()+"见面了。");
    }
    public void business()
    {
        System.out.println(myCompany.getName()+"With the Stars"+myStar.getName()+"Negotiate business."); }} // class Star {private String name; Star(String name) { this.name=name; } public StringgetName()
    {
        returnname; }} // class fan {private String name; Fans(String name) { this.name=name; } public StringgetName()
    {
        returnname; Private String name; private String name; Company(String name) { this.name=name; } public StringgetName()
    {
        returnname; }}Copy the code


The running results of the program are as follows:

Fans Han Cheng and star Ruby Lin met. China Media corp. talks business with star Ruby Lin.Copy the code