Small talk on design patterns (12) - Dimit's law

Column introduction

Column address

link

Column introduction

It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Insert image description here

Demeter's Law

The Law of Demeter, also known as the Least Knowledge Principle, is an object-oriented design principle that emphasizes that an object should minimize interdependence with other objects.
Insert image description here

main idea

Minimize the interdependence between objects and reduce the coupling between objects. Specifically, it emphasizes that an object should only interact with its immediate friends and not with unfamiliar objects.

"Friends" here refer to

Insert image description here

the current object itself

An object can call its own methods because it understands its own structure and behavior.

The object of the current object passed in as a parameter

An object can call methods on the object passed in as a parameter because it understands the structure and behavior of the object passed in.

The object directly referenced by the member variables of the current object

An object can call methods of objects directly referenced by its member variables because it understands the structure and behavior of the objects referenced by the member variables.

Target

Reduce the coupling between objects and improve the maintainability, scalability and reusability of the system. By limiting direct interactions between objects, dependencies between objects are reduced, making the system more flexible, easier to modify and test.

Following Demeter's Law can make the design of the system more modular. Each object only needs to focus on its own responsibilities without knowing the internal details of other objects. This can reduce the complexity of the system and improve the readability and maintainability of the code. At the same time, Demeter's Law also helps to improve the scalability of the system, because it reduces the direct dependence between objects, and only a small number of objects need to be modified when adding new functions.
Insert image description here

Java program implementation

// 定义一个学生类
class Student {
    
    
    private String name;

    public Student(String name) {
    
    
        this.name = name;
    }

    public String getName() {
    
    
        return name;
    }
}

// 定义一个班级类
class Class {
    
    
    private String className;
    private List<Student> students;

    public Class(String className, List<Student> students) {
    
    
        this.className = className;
        this.students = students;
    }

    public String getClassName() {
    
    
        return className;
    }

    public List<Student> getStudents() {
    
    
        return students;
    }
}

// 定义一个学校类
class School {
    
    
    private String schoolName;
    private List<Class> classes;

    public School(String schoolName, List<Class> classes) {
    
    
        this.schoolName = schoolName;
        this.classes = classes;
    }

    public String getSchoolName() {
    
    
        return schoolName;
    }

    public List<Class> getClasses() {
    
    
        return classes;
    }
}

// 客户端代码
public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 创建学生对象
        Student student1 = new Student("Tom");
        Student student2 = new Student("Jerry");

        // 创建班级对象
        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        Class class1 = new Class("Class1", students);

        // 创建学校对象
        List<Class> classes = new ArrayList<>();
        classes.add(class1);
        School school = new School("School1", classes);

        // 输出学校的名称和班级的名称
        System.out.println("School Name: " + school.getSchoolName());
        for (Class c : school.getClasses()) {
    
    
            System.out.println("Class Name: " + c.getClassName());
        }

        // 输出班级中的学生姓名
        for (Class c : school.getClasses()) {
    
    
            for (Student s : c.getStudents()) {
    
    
                System.out.println("Student Name: " + s.getName());
            }
        }
    }
}

program analysis

The relationship between student class, class class and school class is consistent with Demeter's law. The student class is only directly related to the class class, the class class is only directly related to the school class, and there is no direct relationship between the student class and the school class. This can reduce the coupling between objects and improve the flexibility and maintainability of the system.

In the client code, we create a school object, then obtain the class object and student object through the school object, and output their information. Through Dimit's law, we can see that the client code only needs to interact with the school class, and does not need to know the internal details of the class class and student class. This can reduce the direct dependence of the client code on other classes and make the system more efficient. Flexible and easy to maintain.
Insert image description here

Summarize

Demeter's Law emphasizes the loose coupling design between objects, which improves the flexibility and maintainability of the system by reducing direct dependencies between objects. Following Demeter's Law can make the system more modular, scalable and testable.

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/133488473