什么是控制反转?这与依赖注入有何关系?

发布于 2024-10-18 03:23:32 字数 621 浏览 3 评论 0原文

可能的重复:
依赖注入 (DI) 和依赖注入之间的区别控制反转(IOC)
控制反转 <依赖注入

嘿,这是一个Scott Hanselman 面试问题。我总觉得这个问题很难回答。 可能这个问题的部分内容可以在堆栈上回答,但总的来说,这是非常重要的。

我还想了解除了 DI 之外的其他形式的 IoC。

有人可以用一些实时例子来解释我吗?

谢谢

Possible Duplicates:
Difference between Dependency Injection (DI) & Inversion of Control (IOC)
Inversion of Control < Dependency Injection

Hey, This is a Scott Hanselman interview question. I always find this question hard to answer.
May be parts of this question could be answered on stack but on a whole this is very important.

I would also like to know other forms of IoC apart from DI.

Can someone explain me with some real time examples.

Thanks

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

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

发布评论

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

评论(2

烟燃烟灭 2024-10-25 03:23:32

依赖注入不是 IoC 的一种形式。控制反转是一种与 DI 完全无关的模式,除了它们通常在某种框架中一起使用这一事实,这导致人们认为它们是同一件事,但实际上它们不是。

依赖注入只是意味着您通过构造函数或一系列设置器将类的依赖项注入其中,而不是在类中实例化它们。它可以在没有任何类型的 IoC 容器的情况下完全手动完成。

手动 DI 的一个非常简单的示例可能是:

import org.apache.http.client.HttpClient;


public class TwitterClient {

    private HttpClient httpClient;

    public TwitterClient(HttpClient httpClient){
        this.httpClient = httpClient;
    }
}

每当您在代码中创建 TwitterClient 时,您还必须创建一个 HttpClient 并将其传入。由于这会相当乏味,因此有一些框架可以使其变得更容易,但正如我所言提到完全可以手动完成。本文涉及手动 DI - http://googletesting。 blogspot.com/2009/01/when-to-use-dependency-injection.html,事实上,一些 Google 产品的早期版本完全是围绕手动 DI 构建的。

这样做的好处是您可以交换实现,因此如果您想传递一个已存根的客户端以进行单元测试,这很容易。否则,就没有真正的方法来对这样的类进行单元测试。

IoC 意味着您拥有某种控制应用程序生命周期的框架。不涉及 DI 的 IoC 的一个很好的例子就是任何 Cocoa apple 框架,它控制 Cocoa 应用程序的生命周期。您实现在应用程序生命周期中的某些点调用的某些方法。这就是为什么它是“好莱坞原则”,不是你调用框架,而是框架调用你。

Dependency injection is not a form of IoC. Inversion of Control is a pattern that's not related to DI at all, except for the fact that they're usually used together in some sort of framework, which lead people to think they're the same thing when they're not.

Dependency injection just means that you inject a class's dependencies into it, through a constructor or a series of setters, rather than instantiating them in the class. It can be done without an IoC container of any sort, completely manually.

A really simple example of manual DI could be:

import org.apache.http.client.HttpClient;


public class TwitterClient {

    private HttpClient httpClient;

    public TwitterClient(HttpClient httpClient){
        this.httpClient = httpClient;
    }
}

Whenever you create a TwitterClient in your code, you would have to also create an HttpClient and pass it in. Since this would be rather tedious, there are frameworks to make it easier, but as I mentioned it's totally possible to do it manually. This article touches on manual DI - http://googletesting.blogspot.com/2009/01/when-to-use-dependency-injection.html, and in fact early versions of some Google produces were built entirely around manual DI.

The benefit here is that you can swap out the implementations, so if you wanted to pass in a stubbed-out client for unit testing purposes it's easy. Otherwise, there would be no real way to unit test a class like that.

IoC means that you've got some sort of framework that's in control of the application's lifecycle. A good example of IoC that doesn't involve DI would be just about any of the Cocoa apple frameworks, that control the lifecycle of a Cocoa app. You implement certain methods that get called at certain points in the lifecycle of the app. That's why it's the "Hollywood principle", you don't call the framework, the framework calls you.

追我者格杀勿论 2024-10-25 03:23:32

对于采访,我会简短地说控制反转是通过将执行顺序与正在执行的实际业务规则分开来将组件相互解耦的能力。一个例子是在 MVC 应用程序中,一个视图调用多个控制器,不关心控制器将做什么,而是控制它们执行的顺序。

依赖注入是使用接口在类之间调用,只有在运行时才会真正调用类。指定(例如在配置 XML 文件中)。 DI 允许在更细粒度的级别上进行测试。它通过将错误与测试隔离来帮助维护。

它们的相似之处在于依赖注入使用控制反转来允许代码调用接口而不关心实际的实现类,只关心接口。依赖注入允许(从示例中)视图控制接口调用的顺序,而不关心实际使用哪个类。

For an interview I would be brief and say inversion of control is the ability to decouple components from each other by separating order of execution with the actual business rules being execute. An example would be in an MVC app, a view calling several controllers, not caring what the controllers will do, but controlling the order they execute in.

Dependency injection is using interfaces to call between classes, and only at runtime will the actual class be specified (for example in a config XML file). DI allows testing to be done at a much more granular level. It helps with maintenance by isolating bugs from testing.

They are similar in that dependency injection uses inversion of control to allow code to call interfaces without caring about the actual implementation class, just the interface. Dependency injection allows (from the example) the view to control the order of the interface calls without caring which class will actually be used.

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