关于dependency inversion principle的理解

本文介绍了依赖倒置原则(DIP),旨在通过使高层模块不依赖于底层模块,而是依赖于抽象来降低系统的耦合度。文章以Homepwner应用为例,详细解释了如何通过依赖倒置来提高代码的健壮性和可维护性,并提到了依赖注入是一种常见的实现依赖倒置的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


首先讲了这个原则的目的是为了 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. 


ItemsViewController is 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 Item instances and to which it can pass new or updated Item instances to be stored persistently 持久的.


This results in a decoupling because ItemsViewController is not dependent on ItemStore. In fact, as long as the store abstraction is

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

 without any changes to ItemsViewController.

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


... 之后再了解!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值