实际上,为什么我要使用鸭子类型或控制反转?

发布于 2024-11-02 06:51:52 字数 1432 浏览 4 评论 0原文

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

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

发布评论

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

评论(5

美胚控场 2024-11-09 06:51:52

即使您没有像评论中暗示的那样“为其他开发人员开发代码”,对接口进行编码也有很多好处。讨论这些好处是一项值得进行的练习,但由于问题要求提供真实世界的示例/用例,因此:

  • 用于测试。   IoC 可以轻松引入模拟和其他简化/测试类,这些类允许测试特定单元,而无需引入项目的所有依赖项。
  • 用于在某些依赖项可用之前实现应用程序的特定部分。 (类似于测试用例)
  • 向不同的用户提供不同的功能集。您可以拥有给定模块的多个“版本”,并向不同类型的客户提供适当的模块:评估版、标准版、高级版甚至 Beta 测试版。只要遵循模块的 API,应用程序的其余部分就会以相同的方式工作,而不必担心所使用的模块的特殊性质。
  • 使用特定的框架。这个例子可能被视为循环引用:“为什么这些框架首先使用 IoC?”,但事实是 IoC 模式在几个库和框架中无处不在,它们本身非常有用。
  • 创建拦截器。虽然拦截器是一种有用的调试设备,但它们也可以用于多种用途。

请注意我如何尝试并仅提供具有非常清晰的用例的示例。请注意,使用 IoC 和相关概念还有许多其他原因(例如,帮助进行模块化设计、提高可读性……),但这些原因/优点更微妙、不太客观,但同样可能是这样重要的!

There are many benefits of coding to an Interface, even if you do not "develop code for other developers" as hinted in your comment. Discussing these benefits is a worthy exercise, but since the question asks for real world examples/use cases, here goes:

  • For testing.   IoC makes it easy to introduce mocks and other simplified/test classes which allow testing particular units without having to bring in all the dependencies of the project into play.
  • For implementing particular portions of the application before some dependencies are available. (similar to the testing case)
  • To deliver different sets of functionality to different users. You can have multiple "editions" of a given module, and deliver the appropriate module to different types of customers: Evaluation edition, Standard Edition, Advanced Edition and even Beta Test Edition. So long as the API of the module is respected, the rest of the application works the same without having to worry about the particular nature of the module in use.
  • To use particular frameworks. This example may be perceived as a circular reference: "Why do these frameworks use IoC in the first place?", but the fact is that the IoC pattern is omnipresent in several libraries and frameworks which are in of themselves very useful.
  • To create interceptors. While interceptor are a useful debug device, they can also be used for so many purposes.

Note how I tried and provide only examples with a very clear use case. Be aware that there are many other reasons to use IoC and related concepts (for example to help with producing a modular design, to improve readability...) but such reasons/advantages are more subtle, less objective, but none less maybe just as important!

人心善变 2024-11-09 06:51:52

据我了解,依赖注入是一种通过控制反转实现的设计模式,它更接近软件设计中的原则。

依赖注入的一个例子是将 Car 和 Engine 分成两个单独的类。汽车需要发动机才能运行,并且可以使用任何发动机运行。然后将引擎的依赖项注入到汽车中。

IoC/Dependency 的好处是类及其用途的清晰分离。一个类可以被另一个类替换,而不会破坏你的代码。依赖注入可以在运行时完成,这样可以轻松地重新配置软件。

As I understand it Dependency Injection is a design pattern which is implemented by Inversion of Control which is closer to principle in software design.

One example of Dependency Injection could be the separation of Car and Engine into two separate classes. The car needs an engine to function and will run with any engine. The engine is then Dependency Injected into the car.

A benefit of IoC/Dependency is clear separation of classes and their uses. A class can be replaced by another without braking your code. Dependency Injection could be done at runtime which allows for easy re-configuration of your software.

流年已逝 2024-11-09 06:51:52

最近的一次经历告诉我,使用 IoC 生活变得多么轻松。在最近的工作中,我必须清理用户提交的 html。项目中很多地方都使用了同样的清洁服务。但以前的正则表达式清理器不适用于某些特定情况。我刚刚使用 JSoup 库编写了一个新的实现,并链接到需要的地方。使用 Spring IoC 切换变得如此简单:)

One recent experience taught me how easy live becomes when using IoC. In a recent work, I had to clean a user submitted html. The same cleaner service is used in many places in the project. But previous regex cleaner isn't working for some particular cases. I just wrote a new implementation with JSoup library and linked where it is needed. The switching become so simple with Spring IoC :)

蝶…霜飞 2024-11-09 06:51:52

引用这篇关于 IoC 主题的文章

使用面向对象的设计原则和功能(例如接口、继承和多态性),IoC 模式可以实现更好的软件设计,促进重用松散耦合和< em>轻松测试软件组件。

强调我的

这些对我来说听起来像是很好的理由。您在寻找更多吗?

至于 Duck Typing,在我看来,目前还没有定论。在某些情况下它有一些很好的好处,但会导致更多的运行时错误可能性。

Quoting this article on the subject of IoC:

Using object-oriented design principles and features such as interface, inheritance, and polymorphism, the IoC pattern enables better software design that facilitates reuse, loose coupling, and easy testing of software components.

(emphasis mine)

These sound like great reasons to me. Are you looking for more?

As for Duck Typing, the jury on that is out still, IMO. It has some nice benefits in some circumstances, but leads to more runtime error possibility.

泪是无色的血 2024-11-09 06:51:52

IoC 基本上可以帮助您在开发时将实例化与代码解耦。因此,运行时对象可以由一些称为容器的外部机构注入(而不是实例化它们的代码)——只要它们遵守接口。编程到接口是设计模式书中的第一个原则。而且优点是很大的。在 IoC 中,它有助于解耦代码。

依赖注入和依赖拉取是 IoC 的两种不同形式。

看看这个链接

一个侧面 - 在java世界中Spring框架甚至允许引入新方法。

不确定 DuckTyping 的情况。

IoC basically help you decouple instantiation from your code at dev time. So the runtime objects can be injected (instead of your code instantiating them) by some outside agency called container - as long as they obey the interface. Program to Interface is the first principle in the design pattern book. And the advantages are great. In IoC, it helps in decoupling your code.

Dependency Injection and Dependency Pull are two different forms of IoC.

Have a look at thislink

A side point - in java world Spring framework even allows introductions of new methods.

Not sure about the DuckTyping.

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