面试官问:“依赖注入的价值是什么?”

发布于 2024-12-09 20:46:12 字数 299 浏览 0 评论 0原文

可能的重复:
什么是依赖注入?

我回答:“你不需要使用 新的运算符。”

面试官的回答:“。就这样?”

这就是我能想到的一切。

如果这不正确,更正确的反应是什么?

Possible Duplicate:
What is dependency injection?

I respond: "You don't need to use the new operator."

Interviewer's response: "<SIGH>. That's it?"

That's all I could come up with.

What would have been a more correct response if that is not correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饭团 2024-12-16 20:46:12

它将组件与其外部依赖项(例如其他库、数据库等)解耦,使您可以轻松更改它们 - 即使在运行时也是如此。

例如,这在自动化测试中很有用,因为您可以通过公共 API 注入模拟对象

It decouples a component from its external dependencies (e.g. other libraries, databases, etc.) allowing you to easily change them - even at runtime.

This can (for example) be useful in automated testing as you can inject mock objects via the public API.

生生不灭 2024-12-16 20:46:12

依赖注入将类与它们所依赖的服务解耦,允许您注册一组服务一次并在整个代码库中使用它们。

这允许您以非侵入方式切换实现,特别是在实现经过单元测试的情况下。

它还允许您针对不同的用例(例如 Web、GUI 与测试)使用相同的类来实现不同的服务实现。

Dependency injection decouples classes from the services they depend on, allowing you to register a set of services once and use them throughout your codebase.

This allows you to switch implementations non-intrusively, especially if the implementations are unit-tested.

It also allows you to put different service implementations with the same classes for different use-cases, such as web vs. GUI vs. testing.

孤蝉 2024-12-16 20:46:12

维基百科文章是一个很好的参考:http://en.wikipedia.org/wiki/Dependency_injection

主要是,它减少了耦合并简化了测试。您可以通过提供模拟而不是真实的类来方便地对类进行单元测试。在面试中,这是一个很好的问题,可以了解应聘者是否熟悉 TDD 良好实践。

The wikipedia article is a good reference: http://en.wikipedia.org/wiki/Dependency_injection.

Mainly, it reduces coupling and eases testing. You can conveniently unit test the class just by feeding it a mock instead of the real class. It's a good question in interviews to see if the candidate is familiar with TDD good practises.

独守阴晴ぅ圆缺 2024-12-16 20:46:12

不要考虑“依赖注入”这个词,而是考虑“工厂列表”。举一个现实世界的例子,想象一下一家拥有数千种不同产品的目录供应公司。它希望使用一种通用方案来将对象保留在库存中并在空时重新排序。如果每种产品都有自己独立的订购方式,并且必须有一个人知道如何订购每种产品,那么经营这样的企业将很困难。然而,企业的运作方式并非如此。相反,将会有一个文件,其中包含每种产品的订购所需的信息和程序。许多产品会使用相同的程序,但少数产品可能需要特殊程序(例如,在下午 2 点到 3 点之间拨打 815-555-6666,询问 Steve,并向他索要带有蓝色端盖的小部件,因为公司通常会在黄色端盖)。

将依赖注入视为将采购指令包含在产品类型列表中的能力。想象一下,拥有这种能力的目录公司可以运行得多么顺利,而如果没有这种能力,任何规模的公司都会多么尴尬。

PS--“依赖注入”,就像“资源分配就是初始化”一样,是一个我不喜欢的短语,因为它的实际含义与组成它的单词关系不大。 DI 的大多数用途似乎与“依赖性”没有太大关系,也与“注入”任何东西没有太大关系。我认为“工厂清单”这个词更长(但音节更短),并且更好地传达了含义。顺便说一句,对于 RIAA,我会替换为“对象生存期与对象范围匹配”。

Rather than thinking in terms of the words "dependency injection", think "list of factories". To use a real-world example, think of a catalog supply firm with thousands of different products. It would like to use a common scheme for keeping objects in inventory and reordering them when empty. It would be hard to run such a business if every product had its own independent means of ordering, and there had to be one person who knew how to order every product. That's not how businesses operate, however. Instead, there will be a file which contains for each product the information and procedures necessary to order it. Many products would use the same procedures, but a few products might require special procedures (e.g. call 815-555-6666 between 2pm and 3pm, ask for Steve, and ask him for widgets with blue end-caps, since the company normally puts on yellow end-caps).

Think of dependency injection as being the ability to include procurement instructions within the list of product types. Think of how smoothly the catalog firm can run with such ability, and how awkward it would be to have a firm of any size without it.

PS--"Dependency injection", like "Resource Allocation Is Initialization", is a phrase I dislike because its practical meaning has little to do with the words that comprise it. Most uses of DI don't seem to have much to do with "dependency", nor with "injecting" anything. I think "list of factories" is a word longer (but syllable shorter) and conveys the meaning much better. Incidentally, for RIAA, I'd substitute "object lifetime matches object scope".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文