迪米特法则介绍
1)对象应对其他对象保持最少的了解
2)类与类关系密切,耦合度越大
3)迪米特法则又叫最少知道原则,一个类对自己依赖的类知道越少越好,对外提供public方法,不对外泄露任何信息
4)只与直接的朋友通信
5)直接的朋友 : 对象间有耦合关系,只要有耦合(A使用B,有依赖,关联,组合,聚合等)关系,我们称出现成员变量,方法参数,方法返回值中称直接朋友,而出现在局部变量则不是直接朋友,陌生的类最好不要以局部变量形式出现在类的内部。
方案一
public class Demeter1 {
public static void main(String[] args) {
Manager manager = new Manager();
//输出总部员工ID和分部的员工信息
manager.printAllEmployee(new CollgeManager());
}
}
/**
* 总部员工
*/
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 CollgeManager {
//返回所有员工
public List<CollegeEmployee> getAllEmployee() {
List<CollegeEmployee> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
CollegeEmployee collegeEmployee = new CollegeEmployee();
collegeEmployee.setId("学院员工ID= " + i);
list.add(collegeEmployee);
}
return list;
}
}
/**
* 总部管理类
* 直接朋友类:Employee(返回值),CollgeManager(方法参数)
* CollegeEmployee不是直接朋友,违背迪米特原则
* 分析 :
* 1. CollegeEmployee不是直接朋友
* 2. 局部变量形式出现在Manager中
* 解决 :
* 封装带自己的类中使用 => 方案二
*/
class Manager {
//返回所有员工
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Employee employee = new Employee();
employee.setId("学院员工ID= " + i);
list.add(employee);
}
return list;
}
void printAllEmployee(CollgeManager collgeManager) {
//分部ID
System.out.println("分部ID*****************************");
List<CollegeEmployee> list1 = collgeManager.getAllEmployee();
for (CollegeEmployee collegeEmployee : list1) {
System.out.println("分部员工ID : " + collegeEmployee.getId());
}
//总部ID
System.out.println("总部员工ID*****************************");
List<Employee> list2 = this.getAllEmployee();
for (Employee employee : list2) {
System.out.println("总部员工ID : " + employee.getId());
}
}
}
方案二
public class Demeter2 {
public static void main(String[] args) {
//输出总部员工
Manager manager = new Manager();
manager.printManagerEmployee();
//输出分部员工
CollgeManager collgeManager = new CollgeManager();
collgeManager.printCollgeManagerEmployee();
}
}
/**
* 总部员工
*/
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 CollgeManager {
//返回所有员工
public List<CollegeEmployee> getAllEmployee() {
List<CollegeEmployee> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
CollegeEmployee collegeEmployee = new CollegeEmployee();
collegeEmployee.setId("总部员工ID= " + i);
list.add(collegeEmployee);
}
return list;
}
void printCollgeManagerEmployee() {
//分部ID
System.out.println("分部ID*****************************");
List<CollegeEmployee> list1 = this.getAllEmployee();
for (CollegeEmployee collegeEmployee : list1) {
System.out.println("分部员工ID : " + collegeEmployee.getId());
}
}
}
/**
* 总部管理类
*/
class Manager {
//返回所有员工
public List<Employee> getAllEmployee() {
List<Employee> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Employee employee = new Employee();
employee.setId("总部员工ID= " + i);
list.add(employee);
}
return list;
}
void printManagerEmployee() {
//总部ID
System.out.println("总部员工ID*****************************");
List<Employee> list2 = this.getAllEmployee();
for (Employee employee : list2) {
System.out.println("总部员工ID : " + employee.getId());
}
}
}
迪米特法则
1)核心是减低类之间的耦合度
2)注意:由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低类之间(对象间)耦合关系,并不是要求完全没有依赖关系。