Of the seven principles of design patterns (Demeter's law, synthesis and Reuse Principle)

catalogue

Dimitt's law

Basic introduction

Application examples

Application example improvement

Precautions and details of Dimitri's law

Composite Reuse Principle

Basic introduction

Design principles and core ideas

Dimitt's law

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, for

No matter how complex the dependent class is, try to encapsulate the logic inside the class. Except for the public method provided, no information will be disclosed

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

5) Direct friend: each object will have 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 call the classes that appear in member variables, method parameters and method return values as direct friends, while the classes that appear in local variables are not direct friends. In other words, unfamiliar classes should not appear inside the class in the form of local variables.

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 the employee ID of the college

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

3) Code demonstration

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
//CollegeEmployee 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 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

3) Improve the code according to Dimitri's law (see the teacher's demonstration)

4) Code demonstration

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 the information (id) of the school headquarters and college employees
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, dimitt's law only requires reducing the coupling relationship between classes (objects), not

Requires no dependencies at all

Composite Reuse Principle

Basic introduction

The principle is to try to use composition / aggregation instead of inheritance

 

Design principles and core ideas

1) Find out what changes may be needed in the application, separate them, and don't mix them with the code that doesn't need to change.

2) Programming for interfaces, not for implementations.

3) Strive for loose coupling design between interactive objects

Keywords: Design Pattern

Added by ziesje on Fri, 21 Jan 2022 08:37:38 +0200