Design pattern (007) -- seven design principles (Demeter's law)

Keywords: Design Pattern

catalogue

1: Demeter principle

1. Basic introduction

2: Application examples

3: Application example improvement

4: Precautions and details of Dimitri's law

1: Demeter principle

(according to Demeter's law, we should avoid the coupling of indirect friends in classes, so as to reduce the coupling between classes)

1. 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) Dimitt's law( Demeter Principle ) Also called Least known principle That is, the less a class knows about the class it depends on, the better. In other words, no matter how complex the dependent class is, try to encapsulate the logic inside the class. public provided externally Method without disclosing any information.
4) There is a simpler definition of Dimitri's Law: Only communicate with direct friends
5) Direct friends As long as each object has a coupling relationship with other objects: There is a coupling relationship between two objects , let's say that there is a difference between the two objects Friendship . There are many ways of coupling, dependency, association, combination and aggregation Wait. Among them, we call it emergence Member variable, method parameter, method return value The class in is Direct friends , and The class that appears in the local variable is not a direct friend . in other words, Strange classes should not appear inside the class in the form of local variables .

2: Application examples

1) There is a school with subordinate colleges and headquarters. Now it is required to print out the employee ID of the school headquarters And college staff id.

 

2) Programming to achieve the above functions, see the code demonstration

package com.zsz.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());
        }
    }
}

3: Application example improvement

1) The problem with the previous design is the SchoolManager In, The CollegeEmployee class is not a direct of the SchoolManager class         friend ( analysis )
2) According to Dimitri's law, you should Avoid coupling such indirect friends in classes
3) Improve the code according to Dimitri's law
package com.zsz.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());
        }
    }
}

4: Precautions and details of Dimitri's law

1) The core of Dimitri's law is Reduce coupling between classes .
2) But be careful : since each class reduces unnecessary dependencies Demeter's law only requires reducing the coupling between classes (objects)       Relationships do not require no dependencies at all .

Posted by McInfo on Wed, 01 Sep 2021 18:00:53 -0700