关于dependency inversion principle的理解

来源:互联网 发布:iphone隐形监控软件 编辑:程序博客网 时间:2024/06/13 01:01


首先讲了这个原则的目的是为了 decouple objects in an application by inverting 反向 certain dependencies between them.


这个原则的应用可以使代码更 robust and maintainable.


The dependency inversion principle states that:


1. High-level objects should not depend on low-level objects. Both should depend on abstractions.


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



另外在 ios-programming-the-big-nerd-ranch-christian-keur 第六版 第十章 P185 中提到的 在Homepwner这个应用当中,针对上述提到的DIP中的高层模块和底层模块分别对应于


A store is a lower-level object that retrives 检索 and save Item instances through details that are only known to that class. 


ItemsViewControlleris a higher-level object that only knows that it will be provided with a utility object (the store) from which it can obtain a list

 of Iteminstances and to which it can pass new or updatedItem instances to be stored persistently 持久的.


This results in a decoupling becauseItemsViewControlleris not dependent onItemStore. In fact, as long as the store abstraction is

 respected,ItemStore could be replaced by another object that fetches  获取 Iteminstances differently (such as by using a web service)

 without any changes to ItemsViewController.

文末还提到了另外一种实现 dependency inversion principle的 a common pattern 叫做: dependency injection


... 之后再了解!




阅读全文
0 0
原创粉丝点击