设计模式六大原则之六:迪米特原则

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());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑吧,高同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值