Dimitt's law
1. One object should keep the least understanding of other objects
2. The closer the relationship between classes, the greater the coupling
3. Dimitri's rule 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, and don't disclose any information except the public method provided
4. There is a simpler definition of Demeter's Law: only communicate 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 combination, association, aggregation, etc. Among them, we call the classes in the member variables, method parameters and method return values as direct friends, while the classes in the local variables are not direct friends, that is, strange classes should not appear inside the class in the form of local variables
Example application
There is a school with a subordinate student and headquarters. Now it is required to print out the employment ID of the school headquarters and the ID of the staff of the college
The code is as follows
//client public class Demeter1{ public static void main(String[] args){ //Create a SchooManger object SchoolManager shchoolManager = new SchoolManager(); //Output the employee id of the college and the employee id of the school headquarters shchoolManager.printAllEmployee(new CollegeManager()); } } //School headquarters staff class Employee{ private String id; public void setId(String id){ this.id = id; } public String getId(){ return id; } } //College staff 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++){ //Ten employees have been added here CollegeEmployee emp = new CollegeEmployee(); emp.setId("College staff id= " + i); list.add(emp); } return list; } } //School management //Analyze the direct friend classes of SchoolManager, such as 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(); for(int i = 0;i <5;i++){ Employee emp = new Employee(); emp.setId("School staff id=" + i); list.add(emp); } return list; } //This method completes the output of school headquarters and college employee information (id) void printAllEmployee(CollegeManager cm){ //Analyze problems //1. College employee 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 employee information List<CollegeEmployee> list1 = cm.getAllEmployee; System.out.println("---Branch employees---"); for(CollegeEmployee e : list1){ System.out.println(e.getId()); } //Obtain the information of 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()); } } }
Example improvement
1. In schoolmanager, the CollegeEmployee class is not a direct friend of the schoolmanager class
2. According to Demeter's law, such coupling of indirect friends in classes should be avoided
Solution
Analyze problems
Encapsulate the employee method of the output college into the CollegeManager;
//client public class Demeter1{ public static void main(String[] args){ System.out.println("Improvement of using Dimitri's law"); //Create a SchooManger object SchoolManager shchoolManager = new SchoolManager(); //Output the employee id of the college and the employee id of the school headquarters shchoolManager.printAllEmployee(new CollegeManager()); } } //School headquarters staff class Employee{ private String id; public void setId(String id){ this.id = id; } public String getId(){ return id; } } //College staff 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++){ //Ten employees have been added here CollegeEmployee emp = new CollegeEmployee(); emp.setId("College staff id= " + i); list.add(emp); } return list; } //Output information of College employees public void printEmployee(){ //Obtain college staff List<CollegeEmployee> list1 = this.getAllEmployee(); System.out.println("---Branch employees---"); for(CollegeEmployee e : list1){ System.out.println(e.getId()); } } } //School management //Analyze the direct friend classes of SchoolManager, such as 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(); for(int i = 0;i <5;i++){ Employee emp = new Employee(); emp.setId("School staff id=" + i); list.add(emp); } return list; } //This method completes the output of school headquarters and college employee information (id) void printAllEmployee(CollegeManager cm){ //Analyze problems //1. Encapsulate the employee method of the output college into the CollegeManager; cm.printEmployee(); //Get college employee information List<CollegeEmployee> list1 = cm.getAllEmployee; System.out.println("---Branch employees---"); for(CollegeEmployee e : list1){ System.out.println(e.getId()); } //Obtain the information of 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()); } } }
Summary of Demeter's law
1. The core of Demeter's law is to reduce the direct coupling of classes
2. Note, however, that since each class reduces unnecessary dependencies, the Dimitri rule
It is only required to reduce the coupling relationship between classes (objects), not to have no dependencies at all