什么是迪米特法则?
(1) 一个对象应该对其他对象保持最少了解
(2) 类与类关系越密切,耦合度越大
(3) 迪米特法则(Demeter Principle)又叫最少知道原则,即一类对自己依赖的类知道越少越好。也就是说,对于被依赖的
类不管有多么复杂,都尽量将逻辑封在在自己类内部。对外除了提供public方法,不对外泄漏任何信息
(4) 迪米特法则还有个更简单的定义: 只与最直接的朋友通信
(5) 最直接的朋友: 每个对象都会与其他对象有耦合的关系,只要两个对象之间有耦合关系,我们就说这两个对象之间
是朋友关系。耦合方式有很多,依赖、关联、组合、聚合等。其中我们称出现成员变量、方法参数、
方法返回值中的类为最直接的朋友,而出现在局部变量中的类不是最直接的朋友。也就是说,陌生类
最好不要以局部变量的形式出现在类的内部
为什么要设计迪米特法则?
(1) 制定规范,尽量减少耦合
案例演示01 - 非迪米特法则
public class Normal {
public static void main(String[] args) {
SchoolManager schoolManager = new SchoolManager();
schoolManager.printAllEmp(new CollegeManager());
}
}
class SchoolEmp {
private Long id;
public SchoolEmp(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class CollegeEmp {
private Long id;
public CollegeEmp(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class CollegeManager {
public List<CollegeEmp> getAllEmp() {
CollegeEmp collegeEmp = null;
List<CollegeEmp> listEmp = new ArrayList<>();
for (int i = 0; i < 10; i++) {
collegeEmp = new CollegeEmp(Long.valueOf(i));
listEmp.add(collegeEmp);
}
return listEmp;
}
}
class SchoolManager {
public List<SchoolEmp> getAllEmp() {
SchoolEmp schoolEmp = null;
List<SchoolEmp> listEmp = new ArrayList<>();
for (int i = 0; i < 10; i++) {
schoolEmp = new SchoolEmp(Long.valueOf(i));
listEmp.add(schoolEmp);
}
return listEmp;
}
public void printAllEmp(CollegeManager collegeManager) {
List<SchoolEmp> schoolEmps = getAllEmp();
for (int i = 0; i < schoolEmps.size(); i++) {
System.out.println(schoolEmps.get(i).getId());
}
List<CollegeEmp> collegeEmps = collegeManager.getAllEmp();
for (int i = 0; i < collegeEmps.size(); i++) {
System.out.println(collegeEmps.get(i).getId());
}
}
}
SchoolManager类的printAllEmp()方法中CollegeEmp不是该类的直接朋友,是以局部变量方式存在,违法了迪米特法则
按照迪米特法则的规定,应该避免与非直接朋友通信
案例演示02 - 迪米特法则
public class Demeter {
public static void main(String[] args) {
SchoolManager schoolManager = new SchoolManager();
schoolManager.printAllEmp(new CollegeManager());
}
}
class SchoolEmp {
private Long id;
public SchoolEmp(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class CollegeEmp {
private Long id;
public CollegeEmp(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class CollegeManager {
public List<CollegeEmp> getAllEmp() {
CollegeEmp collegeEmp = null;
List<CollegeEmp> listEmp = new ArrayList<>();
for (int i = 0; i < 10; i++) {
collegeEmp = new CollegeEmp(Long.valueOf(i));
listEmp.add(collegeEmp);
}
return listEmp;
}
public void printCollegeEmp() {
List<CollegeEmp> collegeEmps = getAllEmp();
for (int i = 0; i < collegeEmps.size(); i++) {
System.out.println(collegeEmps.get(i).getId());
}
}
}
class SchoolManager {
public List<SchoolEmp> getAllEmp() {
SchoolEmp schoolEmp = null;
List<SchoolEmp> listEmp = new ArrayList<>();
for (int i = 0; i < 10; i++) {
schoolEmp = new SchoolEmp(Long.valueOf(i));
listEmp.add(schoolEmp);
}
return listEmp;
}
public void printAllEmp(CollegeManager collegeManager) {
List<SchoolEmp> schoolEmps = getAllEmp();
for (int i = 0; i < schoolEmps.size(); i++) {
System.out.println(schoolEmps.get(i).getId());
}
collegeManager.printCollegeEmp();
}
}
去除了耦合,在CollegeManager类中直接实现对应的打印员工ID方法,避免非直接朋友出现在SchoolManager中