XIV Demeter's Law -- object oriented design principle

In the previous sections, the principles of object-oriented design are introduced in detail Opening and closing principle,Richter substitution principle,Dependency Inversion Principle,Single responsibility principle and Interface isolation principle , this section will introduce the Dimitri rule in detail.

Definition of Demeter's law

Law of Demeter (LoD), also known as the Least Knowledge Principle (LKP), originated from a research project called Demeter of Northeast University in 1987. It was proposed by Ian Holland and popularized by Booch, one of the founders of UML, Later, it was widely known because it was mentioned in the classic book the practical programmer.

The definition of Dimitri's law is: Talk only to your immediate friends and not to strangers. It means that if two software entities do not need to communicate directly, there should be no direct mutual call, and the call can be forwarded through a third party. Its purpose is to reduce the coupling between classes and improve the relative independence of modules.

"Friend" in Dimitri's Law refers to the current object itself, the member object of the current object, the object created by the current object, the method parameters of the current object, etc. these objects are associated, aggregated or combined with the current object, and the methods of these objects can be accessed directly.

Advantages of Dimitri's law

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

  1. It reduces the coupling between classes and improves the relative independence of modules.
  2. Because the affinity is reduced, the reusability of classes and the scalability of the system are improved.


However, the excessive use of Dimitri's law will make the system produce a large number of intermediary classes, which will increase the complexity of the system and reduce the communication efficiency between modules. Therefore, it is necessary to weigh repeatedly when using Dimitri's law to ensure high cohesion and low coupling and clear structure of the system.

Implementation of Dimitri's law

According to the definition and characteristics of Demeter's law, it emphasizes the following two points:

  1. From the perspective of dependencies, only the objects that should be relied on.
  2. From the perspective of the dependent, only the methods that should be exposed are exposed.


Therefore, we should pay attention to the following 6 points when using Dimitri's law.

  1. In the division of classes, weakly coupled classes should be created. The weaker the coupling between classes, the more conducive it is to achieve the goal of reusability.
  2. In the design of class structure, the access rights of class members should be reduced as much as possible.
  3. In class design, priority is given to setting a class as an invariant class.
  4. In terms of references to other classes, minimize the number of references to other objects.
  5. Instead of exposing the attribute members of the class, the corresponding accessors (set and get methods) should be provided.
  6. Use Serializable features with caution.


[example 1] examples of the relationship between stars and brokers.

Analysis: because stars devote themselves to art, many daily affairs are handled by brokers, such as meeting with fans, business negotiation with media companies, etc. The agents here are friends of stars, while fans and media companies are strangers, so it is suitable to use Dimitri's law. Its class diagram is shown in Figure 1.
 


Figure 1 Relationship between stars and brokers


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("Lin Xinru"));
        agent.setFans(new Fans("Fan Han Cheng"));
        agent.setCompany(new Company("China Media Co., Ltd"));
        agent.meeting();
        agent.business();
    }
}

//agent
class Agent {
    private Star myStar;
    private Fans myFans;
    private Company myCompany;

    public void setStar(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 stars" + myStar.getName() + "We met.");
    }

    public void business() {
        System.out.println(myCompany.getName() + "With stars" + myStar.getName() + "Negotiate business.");
    }
}

//Star
class Star {
    private String name;

    Star(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

//fans
class Fans {
    private String name;

    Fans(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

//Media company
class Company {
    private String name;

    Company(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}


The running results of the program are as follows:

Fan Han Cheng met with star Lin Xinru.
China Media Co., Ltd. negotiated business with star Lin Xinru.

Keywords: Java

Added by magicrobotmonkey on Mon, 24 Jan 2022 03:45:37 +0200