1. Basic introduction
- An object should know the least about another object
- The closer the relationship between classes is, the greater the coupling degree is
- The Demeter Principle, also known as the least known Principle, states that a class knows as little as possible about the classes it depends on. That is, no matter how complex the dependent classes are, try to encapsulate the logic inside the classes. Do not disclose any information except to provide public methods
- Demeter’s rule has a simple definition: only communicate with direct friends
- Direct friends: Every object is coupled to other objects, and as long as two objects are coupled to each other, they are said to be friends. There are many ways of coupling: dependency, association, composition, aggregation, etc. Where, 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. That is, unfamiliar classes are best kept from appearing inside the class as local variables
2. Application cases
-
Example Print the IDS of employees in the headquarters and college
-
Basic implementation
public class Demeter { public static void main(String[] args) { // Create a SchoolManager object SchoolManager schoolManager = new SchoolManager(); // Output the staff ID of the school and staff information of the school headquarters schoolManager.printAllEmployee(newCollegeManager()); }}// School headquarters staff class class Employee { private String id; public void setId(String id) { this.id = id; } public String getId(a) { returnid; }}// Staff of the college class CollegeEmployee { private String id; public void setId(String id) { this.id = id; } public String getId(a) { returnid; }}// School of Management staff management class CollegeManager { // Return all staff of the college public List<CollegeEmployee> getAllEmployee(a) { List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); for (int i = 0; i < 10; i++) { // Here we add 10 employees to the list CollegeEmployee emp = new CollegeEmployee(); emp.setId("College Employee ID =" + i); list.add(emp); } returnlist; }}// School management class SchoolManager { // Go back to the school headquarters public List<Employee> getAllEmployee(a) { List<Employee> list = new ArrayList<Employee>(); for (int i = 0; i < 5; i++) { // Here we add 5 employees to the list Employee emp = new Employee(); emp.setId("School headquarters employee ID =" + i); list.add(emp); } return list; } // This method completes the output of school headquarters and school staff information (ID). void printAllEmployee(CollegeManager sub) { // Get the college staff List<CollegeEmployee> list1 = sub.getAllEmployee(); System.out.println("------------ College staff ------------"); for (CollegeEmployee e : list1) { System.out.println(e.getId()); } // Obtain the school headquarters staff List<Employee> list2 = this.getAllEmployee(); System.out.println("------------ School Headquarters staff ------------"); for(Employee e : list2) { System.out.println(e.getId()); }}}Copy the code
CollegeEmployee is not a direct friend. CollegeEmployee is a stranger class. This violates Demeter’s rule and needs to package the CollegeEmployee method to CollegeManager
-
Follow Demeter’s principles to improve the code
public class Demeter { public static void main(String[] args) { // Create a SchoolManager object SchoolManager schoolManager = new SchoolManager(); // Output the staff ID of the school and staff information of the school headquarters schoolManager.printAllEmployee(newCollegeManager()); }}// School headquarters staff class class Employee { private String id; public void setId(String id) { this.id = id; } public String getId(a) { returnid; }}// Staff of the college class CollegeEmployee { private String id; public void setId(String id) { this.id = id; } public String getId(a) { returnid; }}// School of Management staff management class CollegeManager { // Return all staff of the college public List<CollegeEmployee> getAllEmployee(a) { List<CollegeEmployee> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { // Here we add 10 employees to the list CollegeEmployee emp = new CollegeEmployee(); emp.setId("College Employee ID =" + i); list.add(emp); } return list; } // Output information about college staff public void printEmployee(a) { // Get the college staff List<CollegeEmployee> list1 = getAllEmployee(); System.out.println("------------ College staff ------------"); for(CollegeEmployee e : list1) { System.out.println(e.getId()); }}}// School management class SchoolManager { // Go back to the school headquarters public List<Employee> getAllEmployee(a) { List<Employee> list = new ArrayList<Employee>(); for (int i = 0; i < 5; i++) { // Here we add 5 employees to the list Employee emp = new Employee(); emp.setId("School headquarters employee ID =" + i); list.add(emp); } return list; } // This method completes the output of school headquarters and school staff information (ID). void printAllEmployee(CollegeManager sub) { sub.printEmployee(); // Obtain the school headquarters staff List<Employee> list2 = this.getAllEmployee(); System.out.println("------------ School Headquarters staff ------------"); for(Employee e : list2) { System.out.println(e.getId()); }}}Copy the code
3. Notes and details
- The core of Demeter’s law is to reduce coupling between classes
- Since each class reduces unnecessary dependencies, Demeter’s rule requires only a reduction in class (object) coupling, not a complete absence of dependencies