目录
1、一个对象应该对其他对象保持最少的了解。
2、类与类关系越密切,耦合度越大。
3、迪米特法则又叫最少知道原则,即一个类对自己依赖的类知道的越少越好,通俗来说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了公共的方法,不对外泄露任何信息。
4、迪米特法则定义:只与直接朋友通信。
4、直接朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间的耦合关系,这两个对象之间是朋友关系。耦合的方式很多,依赖,关联,组合,聚合等。称出现成员变量,方法参数,方法返回值中的类为直接朋友,而出现在局部变量中的类不是直接的朋友,陌生的类最好不要以局部变量的形式出现在类的内部。
违反迪米特法则的示例代码:
- public class Demeter {
- public static void main(String[] args) {
- SchoolManger schoolManger = new SchoolManger();
- schoolManger.printAllEmployee(new CollegeManger() );
- }
- }
- //学校总部员工
- class Employee{
- private String id;
- public void setId(String id){
- this.id = id;
- }
- public String getId(){
- return id;
- }
- }
- //学院员工类
- class CollegeEmployee{
- private String id;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
- }
- //管理学院员工的管理类
- class CollegeManger{
- //返回学院的所有员工
- public List
getAllEmployee(){ - ArrayList
collegeEmployeeArrayList = new ArrayList<>(); - for (int i = 0; i <10 ; i++) {
- CollegeEmployee collegeEmployee = new CollegeEmployee();
- collegeEmployee.setId("学院员工的id="+i); //添加学院员工
- collegeEmployeeArrayList.add(collegeEmployee);
- }
- return collegeEmployeeArrayList;
- }
- }
- //学校管理类
- //SchoolManger类的直接朋友类有:Employee,CollegeManger
- class SchoolManger {
- //返回学校总部的员工
- public List
getAllEmployee(){ - ArrayList
employeeArrayList = new ArrayList<>(); - for (int i = 0; i <5 ; i++) {
- Employee employee = new Employee();
- employee.setId("学校总部的员工id=" + i);
- employeeArrayList.add(employee);
- }
- return employeeArrayList;
- }
- //输出学校总部和学院员工信息
- public void printAllEmployee(CollegeManger collegeManger){
- //获取到学院员工
- /**
- * 1、CollegeEmployee不是SchoolManger直接朋友
- * 2、CollegeEmployee是以局部变量的方式出现在SchoolManger
- * 3、违反了迪米特法则
- */
- List
allEmployee = collegeManger.getAllEmployee(); - System.out.println("--------学院员工--------");
- for (CollegeEmployee employee: allEmployee) {
- System.out.println(employee.getId());
- }
- //获取到学校总部员工
- List
allEmployee1 = this.getAllEmployee(); - System.out.println("--------学院总部员工--------");
- for (Employee employee1: allEmployee1) {
- System.out.println(employee1.getId());
- }
- }
- }
使用迪米特法则的示例代码:
- public class Demeter {
- public static void main(String[] args) {
- SchoolManger schoolManger = new SchoolManger();
- schoolManger.printAllEmployee(new CollegeManger() );
- }
- }
- //学校总部员工
- class Employee{
- private String id;
- public void setId(String id){
- this.id = id;
- }
- public String getId(){
- return id;
- }
- }
- //学院员工类
- class CollegeEmployee{
- private String id;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
- }
- //管理学院员工的管理类
- class CollegeManger{
- //返回学院的所有员工
- public List
getAllEmployee(){ - ArrayList
collegeEmployeeArrayList = new ArrayList<>(); - for (int i = 0; i <10 ; i++) {
- CollegeEmployee collegeEmployee = new CollegeEmployee();
- collegeEmployee.setId("学院员工的id="+i); //添加学院员工
- collegeEmployeeArrayList.add(collegeEmployee);
- }
- return collegeEmployeeArrayList;
- }
- //输出学院员工信息
- public void printEmployee(){
- List
allEmployee = getAllEmployee(); - System.out.println("--------学院员工--------");
- for (CollegeEmployee employee: allEmployee) {
- System.out.println(employee.getId());
- }
- }
- }
- //学校管理类
- //SchoolManger类的直接朋友类有:Employee,CollegeManger
- class SchoolManger {
- //返回学校总部的员工
- public List
getAllEmployee(){ - ArrayList
employeeArrayList = new ArrayList<>(); - for (int i = 0; i <5 ; i++) {
- Employee employee = new Employee();
- employee.setId("学校总部的员工id=" + i);
- employeeArrayList.add(employee);
- }
- return employeeArrayList;
- }
- //输出学校总部和学院员工信息
- public void printAllEmployee(CollegeManger collegeManger){
- //获取到学院员工
- collegeManger.printEmployee();
- //获取到学校总部员工
- List
allEmployee1 = this.getAllEmployee(); - System.out.println("--------学院总部员工--------");
- for (Employee employee1: allEmployee1) {
- System.out.println(employee1.getId());
- }
- }
- }
1、迪米特法则的核心是降低类之间的耦合
2、由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低类之间的耦合,并不是要求完全没有依赖关系。