有没有人有很好的类比依赖注入? [英] Does anyone have a good analogy for dependency injection?
问题描述
我已经阅读了很多有关依赖注入的文章,并且观看了很多视频,但是我仍然无法自拔。有人有很好的比喻来解释吗?
I have read a lot of articles on Dependency Injection as well as watched a lot of videos, but I still can't get my head around it. Does anyone have a good analogy to explain it?
我看过了敏捷秋季的截图的第一部分,仍然有点困惑。
I watched the first part of the Autumn of Agile screencast and still was a little confused.
推荐答案
类比 ?我会给它一个打击...你的CD播放器立体声是无用的,没有CD上的音乐...(它依赖于CD)。如果他们使用CD内置CD播放器,那么它会很快无聊...
Analogy? I'll give it a whack... Your CD Player stereo is useless without a CD with music on it... (It's dependent on the CD). If they built CD Players with the CD already in it, it would get boring very quickly...
所以他们构建它们,以便您可以注入CD它依赖于它)进入播放器。这样,您可以每次注入不同的,并获得不同的行为(音乐),取决于您注入哪一个。
So they build them so you can "inject" the CD, (on which it is dependent) into the player. That way you can inject a different one each time, and get "different" behavior (music) dependent on which one you inject.
唯一的要求是CD必须是兼容 与 界面 。 (您不能在1992年CD播放机中播放蓝光光盘。)
The only requirement is that the CD must be compatible with the interface defined by the player. (You can't play a blue-ray disk in a 1992 CD player.)
这篇关于有没有人有很好的类比依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!