1.1 基本介绍


1) 一个对象应该对其他对象保持最少的了解

2) 类与类关系越密切,耦合度越大

3) 迪米特法则(Demeter Principle)又叫最少知道原则,即一个类对自己依赖的类知道的 越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内 部。对外除了提供的public 方法,不对外泄露任何信息

4) 迪米特法则还有个更简单的定义:只与直接的朋友通信

5) 直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系, 我们就说这两个对象之间是朋友关系。耦合的方式很多,依赖,关联,组合,聚合 等。其中,我们称出现成员变量,方法参数,方法返回值中的类为直接的朋友,而 出现在局部变量中的类不是直接的朋友。也就是说,陌生的类最好不要以局部变量 的形式出现在类的内部。

1.2 应用实例


1) 有一个学校,下属有各个学院和 总部,现要求打印出学校总部员 工ID和学院员工的id

2) 编程实现上面的功能, 看代码演示

image.png

3)代码演示

  1. package com.atguigu.principle.demeter;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. //客户端
  5. public class Demeter1 {
  6. public static void main(String[] args) {
  7. //创建了一个 SchoolManager 对象
  8. SchoolManager schoolManager = new SchoolManager();
  9. //输出学院的员工id 和 学校总部的员工信息
  10. schoolManager.printAllEmployee(new CollegeManager());
  11. }
  12. }
  13. //学校总部员工类
  14. class Employee {
  15. private String id;
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19. public String getId() {
  20. return id;
  21. }
  22. }
  23. //学院的员工类
  24. class CollegeEmployee {
  25. private String id;
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29. public String getId() {
  30. return id;
  31. }
  32. }
  33. //管理学院员工的管理类
  34. class CollegeManager {
  35. //返回学院的所有员工
  36. public List<CollegeEmployee> getAllEmployee() {
  37. List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
  38. for (int i = 0; i < 10; i++) { //这里我们增加了10个员工到 list
  39. CollegeEmployee emp = new CollegeEmployee();
  40. emp.setId("学院员工id= " + i);
  41. list.add(emp);
  42. }
  43. return list;
  44. }
  45. }
  46. //学校管理类
  47. //分析 SchoolManager 类的直接朋友类有哪些 Employee、CollegeManager
  48. //CollegeEmployee 不是 直接朋友 而是一个陌生类,这样违背了 迪米特法则
  49. class SchoolManager {
  50. //返回学校总部的员工
  51. public List<Employee> getAllEmployee() {
  52. List<Employee> list = new ArrayList<Employee>();
  53. for (int i = 0; i < 5; i++) { //这里我们增加了5个员工到 list
  54. Employee emp = new Employee();
  55. emp.setId("学校总部员工id= " + i);
  56. list.add(emp);
  57. }
  58. return list;
  59. }
  60. //该方法完成输出学校总部和学院员工信息(id)
  61. void printAllEmployee(CollegeManager sub) {
  62. //分析问题
  63. //1. 这里的 CollegeEmployee 不是 SchoolManager的直接朋友
  64. //2. CollegeEmployee 是以局部变量方式出现在 SchoolManager
  65. //3. 违反了 迪米特法则
  66. //获取到学院员工
  67. List<CollegeEmployee> list1 = sub.getAllEmployee();
  68. System.out.println("------------学院员工------------");
  69. for (CollegeEmployee e : list1) {
  70. System.out.println(e.getId());
  71. }
  72. //获取到学校总部员工
  73. List<Employee> list2 = this.getAllEmployee();
  74. System.out.println("------------学校总部员工------------");
  75. for (Employee e : list2) {
  76. System.out.println(e.getId());
  77. }
  78. }
  79. }

1.3 应用实例改进


1) 前面设计的问题在于SchoolManager中,CollegeEmployee类并不是 SchoolManager类的直接朋友 (分析)

2) 按照迪米特法则,应该避免类中出现这样非直接朋友关系的耦合

3) 对代码按照迪米特法则 进行改进

4) 代码演示

  1. package com.atguigu.principle.demeter.improve;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. //客户端
  5. public class Demeter1 {
  6. public static void main(String[] args) {
  7. System.out.println("~~~使用迪米特法则的改进~~~");
  8. //创建了一个 SchoolManager 对象
  9. SchoolManager schoolManager = new SchoolManager();
  10. //输出学院的员工id 和 学校总部的员工信息
  11. schoolManager.printAllEmployee(new CollegeManager());
  12. }
  13. }
  14. //学校总部员工类
  15. class Employee {
  16. private String id;
  17. public void setId(String id) {
  18. this.id = id;
  19. }
  20. public String getId() {
  21. return id;
  22. }
  23. }
  24. //学院的员工类
  25. class CollegeEmployee {
  26. private String id;
  27. public void setId(String id) {
  28. this.id = id;
  29. }
  30. public String getId() {
  31. return id;
  32. }
  33. }
  34. //管理学院员工的管理类
  35. class CollegeManager {
  36. //返回学院的所有员工
  37. public List<CollegeEmployee> getAllEmployee() {
  38. List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
  39. for (int i = 0; i < 10; i++) { //这里我们增加了10个员工到 list
  40. CollegeEmployee emp = new CollegeEmployee();
  41. emp.setId("学院员工id= " + i);
  42. list.add(emp);
  43. }
  44. return list;
  45. }
  46. //输出学院员工的信息
  47. public void printEmployee() {
  48. //获取到学院员工
  49. List<CollegeEmployee> list1 = getAllEmployee();
  50. System.out.println("------------学院员工------------");
  51. for (CollegeEmployee e : list1) {
  52. System.out.println(e.getId());
  53. }
  54. }
  55. }
  56. //学校管理类
  57. //分析 SchoolManager 类的直接朋友类有哪些 Employee、CollegeManager
  58. //CollegeEmployee 不是 直接朋友 而是一个陌生类,这样违背了 迪米特法则
  59. class SchoolManager {
  60. //返回学校总部的员工
  61. public List<Employee> getAllEmployee() {
  62. List<Employee> list = new ArrayList<Employee>();
  63. for (int i = 0; i < 5; i++) { //这里我们增加了5个员工到 list
  64. Employee emp = new Employee();
  65. emp.setId("学校总部员工id= " + i);
  66. list.add(emp);
  67. }
  68. return list;
  69. }
  70. //该方法完成输出学校总部和学院员工信息(id)
  71. void printAllEmployee(CollegeManager sub) {
  72. //分析问题
  73. //1. 将输出学院的员工方法,封装到CollegeManager
  74. sub.printEmployee();
  75. //获取到学校总部员工
  76. List<Employee> list2 = this.getAllEmployee();
  77. System.out.println("------------学校总部员工------------");
  78. for (Employee e : list2) {
  79. System.out.println(e.getId());
  80. }
  81. }
  82. }

1.4 迪米特法则注意事项和细节


1) 迪米特法则的核心是降低类之间的耦合

2) 但是注意:由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低 类间(对象间)耦合关系, 并不是要求完全没有依赖关系