迪米特法则(Demeter Principle)
基本介绍
- 一个对象应该对其他对象保持最少的了解
- 类与类关系越密切,耦合度越大
- 迪米特法则(Demeter Principle)又叫最少知道原则,即一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了提供的 public 方法,不对外泄露任何信息
- 迪米特法则还有个简单的定义:只与直接朋友的通信
- 直接的朋友:每一个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式很多,依赖,关联,组合,聚合等。其中,我们称出现成员变量,方法参数,方法返回值中的类为直接的朋友,而出现在局部变量中的类不是直接的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的内部。
案例
有一个学校,下属有各个学院和总部,现要求打印出学校总部员工 ID 和学院员工的 ID
方法一
看示例代码思考:
package com.lango.principle.demeter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lango
* @version 1.0
*/
// 客户端
public class Demeter1 {
public static void main(String[] args) {
// 创建一个 SchoolManager 对象
SchoolManager schoolManager = new SchoolManager();
// 输出学院的员工 id 和 学校总部的员工 id
schoolManager.printAllEmployee(new CollegeManager());
}
}
// 学校总部员工类
class Employee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
// 学院员工类
class CollegeEmployee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
// 管理学院员工的管理类
class CollegeManager{
// 返回学院的所有员工
public List<CollegeEmployee> getAllEmployee(){
List<CollegeEmployee> list = new ArrayList<>();
for (int i = 0; i < 10; i++) { // 这里增加了 10 个员工到 list
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工id = " + i);
list.add(emp);
}
return list;
}
}
// 管理学校总部员工的管理类
// 分析 SchoolManager 类的直接朋友类有 Employee、CollegeManager
// CollegeEmployee 不是直接朋友,这样违背了迪米特法则
class SchoolManager{
// 返回学校总部的所有员工
public List<Employee> getAllEmployee(){
ArrayList<Employee> list = new ArrayList<>();
for (int i = 0; i < 5; i++) { // 这里增加了 5 个员工到 list
Employee emp = new Employee();
emp.setId("学校总部员工id = " + i);
list.add(emp);
}
return list;
}
// 该方法完成输出学校总部和学院员工信息(id)
void printAllEmployee(CollegeManager sub){
// 分析问题
// 1. 这里的 CollegeEmployee 不是 SchoolManager 的直接朋友
// 2. CollegeEmployee 是以局部变量方式出现在 SchoolManager
// 3. 违反了迪米特法则
// 获取到学院员工
List<CollegeEmployee> list1 = sub.getAllEmployee();
System.out.println("----------学院员工----------");
for (CollegeEmployee e : list1) {
System.out.println(e.getId());
}
// 获取到学校总部员工
List<Employee> list2 = this.getAllEmployee();
System.out.println("----------学校总部员工----------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
- 分析 SchoolManager 类的直接朋友类有 Employee、CollegeManager
- CollegeEmployee 不是直接朋友,这样违背了迪米特法则
分析问题:
- 这里的 CollegeEmployee 不是 SchoolManager 的直接朋友
- CollegeEmployee 是以局部变量方式出现在 SchoolManager
- 违反了迪米特法则
方法二
改进:
package com.lango.principle.demeter.improve;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lango
* @version 1.0
*/
// 客户端
public class Demeter2 {
public static void main(String[] args) {
System.out.println("使用迪米特法则的改进~~~");
// 创建一个 SchoolManager 对象
SchoolManager schoolManager = new SchoolManager();
// 输出学院的员工 id 和 学校总部的员工 id
schoolManager.printAllEmployee(new CollegeManager());
}
}
// 学校总部员工类
class Employee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
// 学院员工类
class CollegeEmployee {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
// 管理学院员工的管理类
class CollegeManager{
// 返回学院的所有员工
public List<CollegeEmployee> getAllEmployee(){
List<CollegeEmployee> list = new ArrayList<>();
for (int i = 0; i < 10; i++) { // 这里增加了 10 个员工到 list
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工id = " + i);
list.add(emp);
}
return list;
}
// 输出学院员工信息
public void printEmployee(){
// 获取到学院员工
List<CollegeEmployee> list1 = getAllEmployee();
System.out.println("----------学院员工----------");
for (CollegeEmployee e : list1) {
System.out.println(e.getId());
}
}
}
// 管理学校总部员工的管理类
// 分析 SchoolManager 类的直接朋友类有 Employee、CollegeManager
// CollegeEmployee 不是直接朋友,这样违背了迪米特法则
class SchoolManager{
// 返回学校总部的所有员工
public List<Employee> getAllEmployee(){
ArrayList<Employee> list = new ArrayList<>();
for (int i = 0; i < 5; i++) { // 这里增加了 5 个员工到 list
Employee emp = new Employee();
emp.setId("学校总部员工id = " + i);
list.add(emp);
}
return list;
}
// 该方法完成输出学校总部和学院员工信息(id)
void printAllEmployee(CollegeManager sub){
// 分析问题
// 1. 将输出学院的员工方法,封装到 CollegeManager
sub.printEmployee();
// // 获取到学院员工
// List<CollegeEmployee> list1 = sub.getAllEmployee();
// System.out.println("----------学院员工----------");
// for (CollegeEmployee e : list1) {
// System.out.println(e.getId());
// }
// 获取到学校总部员工
List<Employee> list2 = this.getAllEmployee();
System.out.println("----------学校总部员工----------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
分析问题
- 将输出学院的员工方法,封装到 CollegeManager
- 满足迪米特法则
迪米特法则注意事项和细节
- 迪米特法则的核心是降低类之间的耦合
- 但是注意:由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低类间(对象间)耦合关系,并不是要求完全没有依赖关系