有没有人对依赖注入有一个很好的比喻? [英] Does anyone have a good analogy for dependency injection?

查看:22
本文介绍了有没有人对依赖注入有一个很好的比喻?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于依赖注入的文章并观看了很多视频,但我仍然无法理解它.有没有人有好的比喻来解释一下?

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆