JAVA语言设计原则之依赖倒置原则详解
小标 2018-08-30 来源 : 阅读 1089 评论 0

摘要:本文主要向大家介绍了JAVA语言设计原则之依赖倒置原则详解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言设计原则之依赖倒置原则详解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

官方定义

A. High-level modules should not depend on low-level modules. Both should depend on abstractions.

高层模块不应该抵赖底层模块,两者应该依赖抽象

B. Abstractions should not depend on details. Details should depend on abstractions.

抽象不应该依赖细节,细节应该依赖抽象

 

场景

我们先以一个严重违反依赖倒置的实现排序功能例子来说明此原则的内容。某天小明的经理要求他说公司订单以Bubble方式来按创建日期来排序,小明接到任务后,马上投入工作,洋洋洒洒写出以下代码:

public class Bubble {

 

    public void doSort() {

        System.out.println("sorting by bubble");

    }

 

}

   

package basic.oop.dip.sort;

 

public class Computer {

 

    public void callSort() {

        Bubble bubble = new Bubble();

        bubble.doSort();

    }

 

}

 

public class Client{

    public static void main(String[] args) {

        Computer computer = new Computer();

        computer.callSort();

    }

}

   

小明觉得运行无误,提交,交给客户,性能良好。

日复一日,订单数量以指数量倍增。客户向经理反应说订单排序超级慢,要求改善性能。于是经验丰富的经理给小明提供了好几种排序方案,要求小明整合到原先的程序。经理给了: 

public class EfficientSortSolution1 {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution1 ");

    }

 

}

  

public class EfficientSortSolution2 {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution2 ");

    }

 

}

     

public class EfficientSortSolution3 {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution3 ");

    }

 

}

   

然而,此时小明发现,自己写的Computer类无法执行经理给的3个方案。惨!只能去修改Computer。 

public class Computer {

 

    public void callSort(String sortType) {

        if(sortType == 0){

            Bubble bubble = new Bubble();

            bubble.doSort();

        }

        if(sortType == 1){

            EfficientSortSolution1 ess1 = new EfficientSortSolution1 ();

            ess1 .doSort();

        }

        if(sortType == 2){

            EfficientSortSolution2 ess2 = new EfficientSortSolution2();

            ess2.doSort();

        }

        if(sortType == 3){

            EfficientSortSolution3 ess3  = new EfficientSortSolution3();

            ess3.doSort();

        }

        //...

public class Client{

    public static void main(String[] args) {

        Computer computer = new Computer();

        computer.callSort(1);

    }

}

   

小明加班加点改完,千辛万苦测试无误后,通知经理,经理说,客户提供了新的排序方法。要求小明在那个基础之上继续整合上去。小明内心:你。。mmp。毫无疑问,小明又要重新修改Computer类的callSort方法,加上新的solution,再重新测试。。。

小明犯了什么错误?

答案很明显,小明的程序严重违反了依赖倒置原则,即高层类严重依赖底层类,Computer严重依赖Bubble等排序类,这两个模块高度耦合,比如我们增加排序方法,势必导致Computer类的修改。一个系统的某一个模块发生变化,牵连一系列模块的连锁更改,不是一个合格程序员写出来的代码,而是我们眼中的垃圾代码。这样的垃圾系统的维护代价是非常巨大的。

依赖倒置粉墨登场

依赖倒置的解决方法是什么呢?我们看定义即知:两者必须依赖对象,并使用依赖注入的手段实现依赖的倒置

得到教训后,学习了依赖倒置后的小明写下,

public interface SortSolution {

 

    public void doSort();

 

}

   

并修改各个Solutions,

public class Bubble extends SortSolution {

 

    public void doSort() {

        System.out.println("sorting by bubble");

    }

 

}

      

public class EfficientSortSolution1  extends SortSolution {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution1 ");

    }

 

}

 

public class EfficientSortSolution2  extends SortSolution {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution2 ");

    }

 

}

   

public class EfficientSortSolution3  extends SortSolution {

 

    public void doSort() {

        System.out.println("sorting by EfficientSortSolution3 ");

    }

 

}

   

最后修改“万恶”的Computer,

   

public class Computer {

 

    private SortSolution solution;

 

    public void setSortSolution(SortSolution solution) {//依赖注入实现依赖倒置

        this.solution = solution;

    }

 

    public void callSort() {

        solution.doSort();

    }

 

}

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程