1 基本介绍
① 一个对象应该对其他对象保持最少的了解;
② 类与类之间的关系越密切,耦合度越大;
③ 迪米特原则(Demeter Principle)又称最少知道原则,即一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了提供的public方法,不对外泄露任何信息。
④ 每个对象都会与别的对象存在耦合关系,耦合的方式有很多种:依赖、关联、组合聚合等。其中我们将出现在成员变量、方法参数、方法返回值中的类称为直接关系类,而出现在局部变量中的类称为非直接关系类。换言之,非直接关系类最好不要以局部变量的形式出现在类的内部。
2 案例演示
现有一个功能:公司总部有5名员工,分部有二十名员工,现在总部需要将这些员工的编号打印出来
// 总部员工
public class CentralOfficeEmployee {
private String eid;
public void setEid(String eid) {this.eid = eid;}
public String getEid() {return eid;}
}
// 分部员工
public class BranchOfficeEmployee {
private String eid;
public void setEid(String eid) {this.eid = eid;}
public String getEid() {return eid;}
}
// 分部员工管理
public class BranchOfficeEmployeeManager {
// 为了演示效果,我们这里进行添加员工
public List<BranchOfficeEmployee> getAllEmployee() {
public List<BranchOfficeEmployee> list = new ArrayList<>();
for (int i=0; i<20; i++) {
BranchOfficeEmployee emp = new BranchOfficeEmployee();
emp.setEid("分部员工id:" + i);
list.add(emp);
}
return list;
}
}
// 总部员工管理
public class CentralOfficeEmployeeManager {
// 为了演示效果,我们这里进行添加员工
public List<CentralOfficeEmployee> getAllEmployee() {
public List<CentralOfficeEmployee> list = new ArrayList<>();
for (int i=0; i<5; i++) {
CentralOfficeEmployee emp = new CentralOfficeEmployee();
emp.setEid("总部员工id:" + i);
list.add(emp);
}
return list;
}
public void printEmployeeID(BranchOfficeEmployeeManager boem) {
List<BranchOfficeEmployee> list1 = boem.getAllEmployee();
for (BranchOfficeEmployee boe : list1) {
System.out.println(boe.getEid());
}
System.out.println("========================================");
List<CentralOfficeEmployee> list2 = this.getAllEmployee();
for (CentralOfficeEmployee coe : list2) {
System.out.println(coe.getEid());
}
}
}
// 我们的客户端功能
public class DemeterCase {
public static void main(String[] args) {
CentralOfficeEmployeeManager coem = new CentralOfficeEmployeeManager();
coem.printEmployeeID(new BranchOfficeEmployeeManager());
}
}
分析:BranchOfficeEmployee这个类是以局部变量的形式出现在CentralOfficeEmployeeManager中,这样就增加了耦合。BranchOfficeEmployee就是CentralOfficeEmployeeManager的非直接关系类,违反了迪米特原则。
优化:将CentralOfficeEmployeeManager中打印分部员工的代码抽取到BranchOfficeEmployeeManager中
// 分部员工管理
public class BranchOfficeEmployeeManager {
// 为了演示效果,我们这里进行添加员工
public List<BranchOfficeEmployee> getAllEmployee() {
public List<BranchOfficeEmployee> list = new ArrayList<>();
for (int i=0; i<20; i++) {
BranchOfficeEmployee emp = new BranchOfficeEmployee();
emp.setEid("分部员工id:" + i);
list.add(emp);
}
return list;
}
public void printEmployeeID() {
List<BranchOfficeEmployee> list1 = this.getAllEmployee();
for (BranchOfficeEmployee boe : list1) {
System.out.println(boe.getEid());
}
}
}
// 总部员工管理
public class CentralOfficeEmployeeManager {
// 为了演示效果,我们这里进行添加员工
public List<CentralOfficeEmployee> getAllEmployee() {
public List<CentralOfficeEmployee> list = new ArrayList<>();
for (int i=0; i<5; i++) {
CentralOfficeEmployee emp = new CentralOfficeEmployee();
emp.setEid("总部员工id:" + i);
list.add(emp);
}
return list;
}
public void printEmployeeID(BranchOfficeEmployeeManager boem) {
boem.printEmployeeID();
System.out.println("========================================");
List<CentralOfficeEmployee> list2 = this.getAllEmployee();
for (CentralOfficeEmployee coe : list2) {
System.out.println(coe.getEid());
}
}
}