13 Demeter's Law -- object oriented design principle

Opening and closing,Richter substitution,Dependency inversion,Single responsibility,Interface isolation,Dimitt,Synthetic multiplexing

In the previous sections, the opening and closing principle, Richter substitution principle, dependency inversion principle, single responsibility principle and interface isolation principle in object-oriented design principles are introduced in detail. This section will introduce Dimiter's law 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 adopted by Booch, one of the founders of UML It was widely known later 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, they should not call each other directly, 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, excessive use of the Dimitri rule will lead to a large number of intermediary classes in the system, which will increase the complexity of the system and reduce the communication efficiency between modules. Therefore, it is necessary to weigh repeatedly when using the Dimitri rule to ensure high cohesion and low coupling while ensuring the 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, we should create weakly coupled classes. The weaker the coupling between classes, the more conducive to achieving 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. brokers here are friends of stars, and 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.

Return to home directory: Comprehensive analysis of 23 design modes (super detailed)

In order to reprint the article, if there is infringement, please contact me for deletion.

This article is reproduced from: Comprehensive analysis of 23 design modes (super detailed)

 

Keywords: Design Pattern

Added by Kaizard on Tue, 02 Nov 2021 05:33:37 +0200