天天看点

设计模式的七大设计原则:其六:迪米特法则

迪米特法则:

基本介绍:

1.一个对象应该对其他对象保持最少的了解

2.类与类关系越密切,耦合度越大

3.迪米特法则(Demeter Principle)又叫最少知道原则,即一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了提供的public方法,不对外泄露任何信息。

4.迪米特法则还有个更简单的定义:只与直接的朋友通信

5.直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间时朋友关系。耦合的方式很多,依赖,组合,关联,聚合等。其中,我们称出现成员变量,方法参数,方法返回值中的类为直接的朋友,而出现在局部变量中的类不是直接的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的内部。

案例:

1.有4个基类,除了Department不是直接的朋友,其他的都是直接的朋友

package demeter;

public class PersonServiceBean {
    private PersonDao personDao;

    public void save(Person person){

    }

    public IDCard getIDCard(Integer personid){
        return null;
    }
    public void modify(){
        Department department = new Department();
    }
}


class IDCard{   
}


class Department{
}



class Person {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



class PersonDao {
}
           

2.有一个学校,下属有各个学院和总部,现要求打印出学校总部员工ID和学院员工的ID

不满足迪米特法则的代码:

package demeter;

import java.util.ArrayList;
import java.util.List;

//客户端
public class Demeter1 {
    public static void main(String[] args) {

    }
}
//学校总部员工
class Employee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
//学院的员工
class ColleageEmployee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
//管理学院员工的管理类
class CollegeManage{
    //返回学院所有员工
    public List<ColleageEmployee> getAllEmployee(){
        List<ColleageEmployee> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //这里增加了10个员工到list
            ColleageEmployee emp = new ColleageEmployee();
            emp.setId("学院员工id = " + i);
            list.add(emp);
        }
        return list;
    }
}
//学校管理类
class SchoolManager{
    //返回学校总部的员工
    public List<Employee> getAllEmployee(){
        List<Employee> list = new ArrayList<>();
        //这里增加了5个员工到list
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工Id=" + i);
            list.add(emp);
        }
        return list;
    }

    void printAllEmployee(CollegeManage sub){
        List<ColleageEmployee> list1 = sub.getAllEmployee();
        System.err.println("------分公司员工-------");
        for (ColleageEmployee colleageEmployee : list1) {
            System.err.println(colleageEmployee.getId());
        }
        List<Employee> list2 = this.getAllEmployee();
        System.err.println("-------学校总部员工-------");
        for (Employee employee : list2) {
            System.err.println(employee.getId());
        }
    }

}

           

3.改进,上面代码的打印代码那里,在SchoolManager中的printAllEmployee方法中直接与ColleageEmployee产生了朋友关系,这不是直接的朋友,这样是不满足迪米特法则的。

改进代码如下:

package demeter;

import java.util.ArrayList;
import java.util.List;

//客户端
public class Demeter1 {
    public static void main(String[] args) {

    }
}
//学校总部员工
class Employee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
//学院的员工
class ColleageEmployee{
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
//管理学院员工的管理类
class CollegeManage{
    //返回学院所有员工
    public List<ColleageEmployee> getAllEmployee(){
        List<ColleageEmployee> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //这里增加了10个员工到list
            ColleageEmployee emp = new ColleageEmployee();
            emp.setId("学院员工id = " + i);
            list.add(emp);
        }
        return list;
    }
    //输出学院员工的信息
    public void printEmployee(){
        //获取到学院员工
        List<ColleageEmployee> list1 = getAllEmployee();
        System.err.println("------分公司员工-------");
        for (ColleageEmployee colleageEmployee : list1) {
            System.err.println(colleageEmployee.getId());
        }
    }
}
//学校管理类
class SchoolManager{
    //返回学校总部的员工
    public List<Employee> getAllEmployee(){
        List<Employee> list = new ArrayList<>();
        //这里增加了5个员工到list
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工Id=" + i);
            list.add(emp);
        }
        return list;
    }

    void printAllEmployee(CollegeManage sub){
        sub.printEmployee();

        List<Employee> list2 = this.getAllEmployee();
        System.err.println("-------学校总部员工-------");
        for (Employee employee : list2) {
            System.err.println(employee.getId());
        }
    }

}

           

继续阅读