1. Basic introduction

  1. One object should have minimal knowledge of other objects

  2. The closer the relationship between classes is, the greater the coupling degree is

  3. 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, for dependent classes, no matter how complex, try to encapsulate the logic inside the class. Do not disclose any information except for the provided public method

  4. There’s a simpler definition of Demeter’s rule: Only communicate with direct friends

  5. Direct friends: Every object is coupled to other objects, and as long as there is a coupling between two objects, we say they are 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 examples

There is a school with various colleges and headquarters under it. It is required to print the ID of the staff of the school headquarters and the ID of the staff of the school

3. Code implementation

3.1 one way

1. Staff of school headquarters

class Employee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId(a) {
		returnid; }}Copy the code

2. College staff

class CollegeEmployee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId(a) {
		returnid; }}Copy the code

3. Management of school staff

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; }}Copy the code

4. 4, Tardiness

Employee and CollegeManager are the direct friends of SchoolManager
//CollegeEmployee is not a direct friend but a stranger class, which violates Demeter's law
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) {
		
		// Analyze the problem
		//1. CollegeEmployee here is not a direct friend of SchoolManager
		//2. CollegeEmployee appears in SchoolManager as a local variable
		//3. Demeter's Law is violated
		
		// 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

3.2 way 2

  1. The CollegeEmployee class is not a direct friend of SchoolManager.

  2. Demeter’s rule is to avoid such indirect friend coupling in classes

1. Staff of school headquarters

class Employee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId(a) {
		returnid; }}Copy the code

2. College staff

class CollegeEmployee {
	private String id;

	public void setId(String id) {
		this.id = id;
	}

	public String getId(a) {
		returnid; }}Copy the code

3. Management of school staff

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);
		}
		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()); }}}Copy the code

4. 4, Tardiness

Employee and CollegeManager are the direct friends of SchoolManager
//CollegeEmployee is not a direct friend but a stranger class, which violates Demeter's law
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) {
		
		// Analyze the problem
		//1. Package the output college staff method to CollegeManager
		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

5. Test

	public static void main(String[] args) {
		System.out.println("~~~ improvement using Demeter's rule ~~");
		// Create a SchoolManager object
		SchoolManager schoolManager = new SchoolManager();
		// Output the staff ID of the school and staff information of the school headquarters
		schoolManager.printAllEmployee(new CollegeManager());

	}

Copy the code

Demeter’s Rule notes and details

  1. The core of Demeter’s law is to reduce coupling between classes
  2. Note, however, that because each class reduces unnecessary dependencies, Demeter’s rule requires only a reduction in coupling between classes (objects), not a complete absence of dependencies