First, Demeter
Basic introduction :
(1) an object should be kept to a minimum understanding of other objects
(2) the closer the relationship between class and class, the greater the degree of coupling
(3) Demeter (Demeter Priciple), also known as the least known principle that a class of their dependent classes know better. That is, for no matter how complex the dependent classes, as much as possible the internal logic is encapsulated in the class. Foreign methods provided in addition to public and not to divulge any information
(4) Demeter, there are more simple definition: only with direct friends communicate
(5) direct friends : Each object has a coupling relationships to other objects, as long as the coupling relationship between two objects, we say that friendship between the two objects. The more coupling mode, dependent, associated, combined, polymerization, etc., which we call occurs member variable, method parameters, return value of the method is a direct friend class , is now out of the local variable class is not a direct friend. In other words, unfamiliar class best not to appear within the class in the form of local variables.
Meaning : reduce the coupling between classes
Second, case presentations
Scene: There is a school, affiliated with various colleges and headquarters, schools are now required to print out the ID headquarters staff and college staff ID
1. Basic code implementation
We defined four categories: Employee (school headquarters staff category), CollegeEmployee (College of employees category), CollegeManager (School of Management, staff management class), SchoolManager (School Management)
of java.util.ArrayList Import; Import java.util.List; / * * * @ClassName: demeter01 * @Description: * @author: xiedong * @date: 2020/3/21 21:17 * / public class demeter01 { public static void main (String [] args) { // create a SchoolManager objects SchoolManager schoolManager = new new SchoolManager (); // employee id and school headquarters output College employee information schoolManager.printAllEmployee ( new new CollegeManager ()); } } // school headquarters staff class class the employee { Private String ID; public void the setId (String ID) { the this .id = ID; } public String getId () { return ID; } } // Staff College class class CollegeEmployee { Private String ID; public void the setId (String ID) { the this .id = the above mentioned id; } public String getId () { return the above mentioned id; } } // School of management, staff management class class CollegeManager { // all employees return to school public List <CollegeEmployee> getAllEmployee () { List <CollegeEmployee> List = new new the ArrayList <CollegeEmployee> (); for ( int I = 0 ; I < 10 ; I ++) { // Here we added 10 employees to List CollegeEmployee EMP = new new CollegeEmployee (); emp.setId ( " INSTITUTE = employee ID " + I); List.add (EMP); } return List; } } // school management class class SchoolManager { //Back to school headquarters staff public List <the Employee> getAllEmployee () { List <the Employee> List = new new ArrayList <the Employee> (); for ( int i = 0 ; i < 5 ; i ++) { // Here we have added 5 employees to List the employee emp = new new the employee (); emp.setId ( " school = headquarters staff of the above mentioned id " + i); list.add (emp); } return List; } // this method completes output schools and colleges headquarters employee information (ID) void printAllEmployee (CollegeManager Sub) { // get to college staff List <CollegeEmployee> List1 = sub.getAllEmployee (); . System OUT .println ( " ------------ ----------- College staff - " ); for (E CollegeEmployee: List1) { the System. OUT .println (e.getId ()); } // Get the school headquarters staff List <the employee> = List2 the this .getAllEmployee (); the System. OUT .println ( " ------------ school headquarters staff ------------ " ); for (the employee E: list2) { . System OUT.println(e.getId()); } } }
Achieve results:
Although the results achieved, but we have to be analyzed as follows SchoolManager categories:
Analysis: direct friends SchoolManager classes: Employee, CollegeManager; familiar friend: CollegeEmployee this violates the Law of Demeter
2, based on an improved version of Demeter
Ways to Improve :
① problem lies directly in front of the design in SchoolManager friends, CollegeEmployee the class is not SchoolManager
② according to the Law of Demeter, should avoid such indirect coupling friendships in the class appear ( to be exported and college staff approach to CollegeManager package in addition to the external public methods and not to divulge any information )
2.1 improved version of the code
public class demeter02 { public static void main (String [] args) { the System. OUT .println ( ' ~~~ ~ using the improved Demeter " ); // creates a SchoolManager objects SchoolManager schoolManager = new new SchoolManager (); // employee id and school headquarters output College employee information schoolManager.printAllEmployee ( new new CollegeManager ()); } } // school headquarters staff class class the employee { Private String id; public void setId (String id) { the this .id = ID; } public String getId () { return ID; } } // Staff College class class CollegeEmployee { Private String ID; public void the setId (String ID) { the this .id = ID; } public String getId ( ) { return ID; } } // School employee management class class CollegeManager { // return all employees INSTITUTE public List <CollegeEmployee> getAllEmployee () { List<CollegeEmployee> List = new new the ArrayList <CollegeEmployee> (); for ( int I = 0 ; I < 10 ; I ++) { // Here we added 10 employees to List CollegeEmployee EMP = new new CollegeEmployee (); emp.setId ( " INSTITUTE = staff ID " + I); List.add (EMP); } return List; } public void printEmployee () { // acquired employee INSTITUTE List <CollegeEmployee> = List1 getAllEmployee (); the System.OUT .println ( " ------------ School staff ------------ " ); for (CollegeEmployee E: List1) { System. OUT .println (E. getId ()); } } } // school management class class SchoolManager { // return to school staff headquarters public List <the employee> getAllEmployee () { List <the employee> List = new new the ArrayList <the employee> (); for ( int I = 0 ; I < . 5 ; I ++) { // here we added to the list of employees 5 Emp = the Employee new new the Employee (); emp.setId ( " School = headquarters staff of the above mentioned id " + i); list.add (emp); } return List; } // This method completes output schools and colleges headquarters employee information (id) void printAllEmployee (CollegeManager Sub) { // employee method of outputting Institute, encapsulated CollegeManager sub.printEmployee (); // Get the school headquarters staff List <the employee> = List2 the this .getAllEmployee (); . the System OUT .println ( " ------------ ------------ school headquarters staff " ); for (Employee e : list2) { System.out.println(e.getId()); } } }
analyse as below:
According to the Law of Demeter avoiding the emergence of such indirect coupling friendships in the class.
Third, the summary
1, Demeter core is reduced coupling between classes
2, since each class reduce unnecessary reliance, therefore only requires Demeter reduce coupling relationship (inter-object) between classes, no dependencies are not required.