美文网首页
依赖倒置原则(Dependency Inversion Prin

依赖倒置原则(Dependency Inversion Prin

作者: 心中翼 | 来源:发表于2019-01-18 16:07 被阅读0次

    依赖倒置原则,看中文翻译导致我有很长时间的误解,总是感觉有依赖关系被反转了一样。直到看了维基百科的英文才明白这种思想是错的,因为英文中有很明确的一句定义就是:““反转”的概念并不意味着低层依赖于高层”。

    With the addition of an abstract layer, both high- and lower-level layers reduce the traditional dependencies from top to bottom. Nevertheless, the "inversion" concept does not mean that lower-level layers depend on higher-level layers. Both layers should depend on abstractions that draw the behavior needed by higher-level layers.

    通过添加抽象层,高层和低层都从上到下减少了传统的依赖关系。然而,“反转”的概念并不意味着低层依赖于高层。这两个层都应该依赖于抽象,抽象可以绘制高级层所需的行为。

    依赖抽象原则有两个概念:
    1.高层模块不应该依赖底层模块,两个都应该依赖抽象。
    2.抽象不应该依赖细节,细节应该依赖抽象。

    用图表示就是:
    传统的分层模式:


    image.png

    依赖倒置原则:


    image.png

    传统分层模式:用户可以用手机或者平台或者电脑实现上网功能,但是他是依赖与具体实现的,因为他必须拥有一部手机或者电脑或者平台才可以。


    image.png

    依赖倒置原则:用户不依赖于具体实现,用户依赖于抽象设备,他可以使用设备的上网功能,但不用在意设备具体是三种中的哪种,不依赖于低层模块,在代码层面体现为,可以灵活的修改与复用低层模块。


    image.png

    相关文章

      网友评论

          本文标题:依赖倒置原则(Dependency Inversion Prin

          本文链接:https://www.haomeiwen.com/subject/ptmndqtx.html