Dimitt's law

Posted by ron814 on Tue, 28 Dec 2021 12:57:27 +0100

Basic introduction

1) One object should have minimal knowledge of other objects

2) The closer the relationship between classes, the greater the degree of coupling

3) The Demeter Principle is also called the least known principle, that is, the less a class knows about the class it depends on, the better. That is, no matter how complex the dependent class is, try to encapsulate the logic inside the class.

No information will be disclosed except the public method provided

4) There is a simpler definition of Dimitri's Law: communicate only with direct friends

5) Direct friends: each object has a coupling relationship with other objects. As long as there is a coupling relationship between two objects, we say that the two objects are friends. There are many ways of coupling, such as dependency, association, combination, aggregation and so on. Among them, we

The class in the member variable, method parameter and method return value is called a direct friend, while the class in the local variable is not a direct friend. In other words, unfamiliar classes should not appear inside the class in the form of local variables.

Code demonstration:

package com.atguigu.principle.demeter;

import java.util.ArrayList;
import java.util.List;

//client
public class Demeter1 {

    public static void main(String[] args) {
        //Created a SchoolManager object
        SchoolManager schoolManager = new SchoolManager();
        //Output the employee id of the college and the employee information of the school headquarters
        schoolManager.printAllEmployee(new CollegeManager());

    }

}


//School headquarters staff
class Employee {
    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}


//Staff of the College
class CollegeEmployee {
    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}


//Management of the staff of the school of management
class CollegeManager {
    //All employees returning to the College
    public List<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        for (int i = 0; i < 10; i++) { //Here we added 10 employees to the list
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("College staff id= " + i);
            list.add(emp);
        }
        return list;
    }
}

//School management

//Analyze which direct friend classes of the SchoolManager class are Employee and CollegeManager
//College employee is not a direct friend, but a strange class, which violates Dimitri's law
class SchoolManager {
    //Employees returning to the school headquarters
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        
        for (int i = 0; i < 5; i++) { //Here we added five employees to the list
            Employee emp = new Employee();
            emp.setId("School headquarters staff id= " + i);
            list.add(emp);
        }
        return list;
    }

    //This method completes the output of school headquarters and college employee information (id)
    void printAllEmployee(CollegeManager sub) {
        
        //Analyze problems
        //1. The CollegeEmployee here is not a direct friend of SchoolManager
        //2. CollegeEmployee appears in SchoolManager as a local variable
        //3. Violation of Dimitri's law
        
        //Get college employees
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("------------College staff------------");
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
        //Get to the staff of the school headquarters
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("------------School headquarters staff------------");
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}

Application example improvement:

1) The problem with the previous design is that in the SchoolManager, the CollegeEmployee class is not a direct friend (analysis) of the SchoolManager} class

2) According to Demeter's law, such coupling of indirect friends in classes should be avoided

Code demonstration:

package com.atguigu.principle.demeter.improve;

import java.util.ArrayList;
import java.util.List;

//client
public class Demeter1 {

    public static void main(String[] args) {
        System.out.println("~~~Improvement of using Dimitri's law~~~");
        //Created a SchoolManager object
        SchoolManager schoolManager = new SchoolManager();
        //Output the employee id of the college and the employee information of the school headquarters
        schoolManager.printAllEmployee(new CollegeManager());

    }

}


//School headquarters staff
class Employee {
    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}


//Staff of the College
class CollegeEmployee {
    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}


//Management of the staff of the school of management
class CollegeManager {
    //All employees returning to the College
    public List<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        for (int i = 0; i < 10; i++) { //Here we added 10 employees to the list
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("College staff id= " + i);
            list.add(emp);
        }
        return list;
    }
    
    //Output information of College employees
    public void printEmployee() {
        //Get college employees
        List<CollegeEmployee> list1 = getAllEmployee();
        System.out.println("------------College staff------------");
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
    }
}

//School management

//Analyze which direct friend classes of the SchoolManager class are Employee and CollegeManager
//College employee is not a direct friend, but a strange class, which violates Dimitri's law
class SchoolManager {
    //Employees returning to the school headquarters
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        
        for (int i = 0; i < 5; i++) { //Here we added five employees to the list
            Employee emp = new Employee();
            emp.setId("School headquarters staff id= " + i);
            list.add(emp);
        }
        return list;
    }

    //This method completes the output of school headquarters and college employee information (id)
    void printAllEmployee(CollegeManager sub) {
        
        //Analyze problems
        //1. Encapsulate the employee method of the output college into the CollegeManager
        sub.printEmployee();
    
        //Get to the staff of the school headquarters
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("------------School headquarters staff------------");
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}

Precautions and details of Dimitri's law

1) The core of dimitt's law is to reduce the coupling between classes

2) But note: since each class reduces unnecessary dependencies, Dimitri's law only requires reducing the coupling between classes, not requiring no dependencies at all

Topics: Design Pattern