DIP与DI与国际奥委会 [英] DIP vs. DI vs. IoC

查看:141
本文介绍了DIP与DI与国际奥委会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关大约2个月我一直在读的一切,我可以找到这3个主题,我还不确定我知道了。


  1. 依赖倒置原则。意味着你应该永远只能依靠接口,而不是他们的实现。如果你的等级取决于其他类,这是很糟糕的,因为它依赖于第二类的细节。如果你的等级取决于接口,这是绝对OK,因为这种依赖只是意味着你的类需要抽象的东西,可以做一些具体的,你真的不关心它的方式。

    因为P中蘸代表原理,我也许应该把它定义是这样的:依赖倒置原则是要求所有code的实体细节仅仅依靠一个原则,他们的真正的必要性

    这是他们真正需要的信息我的意思是最简单的情形接口。我还用了实体,以强调该DIP也可以适用于程序和任何其他,不仅类


  2. 依赖关系注入。这是只适用于启用DI-实体。启用DI-实体是它是在不改变它的内部配置其行为开放的实体。有两个基本类型的注射(在谈论类时):


    • 构造注射 - 是当你通过所有必需的抽象方式,对象只是它要建造的那一刻

    • Setter注入 - 是对象已经被创建后,当你澄清所要求的方面

    所以,这个定义可能是像下面这样:依赖注入是通过了抽象的方式,即的实体的过程中真正的需要这些细节

    这是真正需要这些细节我的意思是最简单的情形接口。在实体字,一如既往,用来强调DI也适用于程序和任何其他。


  3. 控制反转。它通常被定义为库和框架之间的区别,因为写程序的任何一种方式,你在过程编程那样等等。这对我来说是最令人困惑的事情。我认为,主要的想法是在这里只是发起任何行动。要么你做只要你想的东西(程序的方式),或者等待,直到有人问你(IoC的方式)。

    我的定义是:国际奥委会是你的程序的执行流程的属性,当你什么也不做,直到的他们的要求你做它

    这正是听起来像好莱坞原则,但我相信,好莱坞原则和国际奥委会都完全同样的想法。


我明白了吗?


解决方案

我就带它是这样的:DIP是的原理的引导我们走向DI。基本上,松耦合的是目标,并且至少有两种方法来实现它。


  • 依赖关系注入

  • 服务定位器

不过,服务定位是一个反模式并无关与DIP 。 DI,然而,这是DIP的正确应用。

的<一个href=\"http://stackoverflow.com/questions/3226605/inversion-of-control-dependency-injection/3227404#3227404\">relationship DI和IOC之间之前已经解释。

BTW,当谈到DI和松耦合,我发现在奠定了术语的领域驱动设计最适用的。基本上,<一个href=\"http://stackoverflow.com/questions/4835046/why-not-use-an-ioc-container-to-resolve-dependencies-for-entities-business-object/4836790#4836790\">Services是唯一对象的种类受到DI 。

For about 2 months I've been reading everything I can find for these 3 topics and I'm not yet sure I got it.

  1. Dependency Inversion Principle. Means you should always only rely on interfaces and not on their implementations. If your class depends on any other class, that's bad, because it depends on that second class' details. If your class depends on interface, that's absolutely OK since this kind of dependence only means that your class needs something abstract that can do something specific and you don't really care the way it does it.

    Since P in "DIP" stands for "Principle", I should probably define it this way: Dependency Inversion Principle is a principle that requires all your code's entities to depend only on details they really need.

    By "details they really need" I mean interfaces for the simplest case. I also used the word "entities" to emphasize that DIP is also applicable to procedures and whatever else, not only to classes.

  2. Dependency Injection. It's only applicable to DI-enabled entities. DI-enabled entity is an entity which is "open" for configuring its behavior without changing its internals. There are 2 basic kinds of injection (when talking about classes):

    • Constructor Injection - is when you pass all the required "abstract details" to the object just by the moment it's about to be constructed.
    • Setter Injection - is when you "clarify" the required aspects after the object has already been created.

    So, the definition is probably like following: Dependency Injection is a process of passing the "abstract details" to the entity that really needs these details.

    By "really needs these details" I mean interfaces for the simplest case. The word "entities" is, as always, used to emphasize that DI is also applicable to procedures and whatever else.

  3. Inversion of Control. It's often defined as "difference between libraries and frameworks", as "writing programs the either way you did in procedural programming" and so forth. That the most confusing thing for me. I believe that main idea here is just about initiating any actions. Either you do something "whenever you want" (Procedural way), or you "wait" until someone asks you (IoC way).

    My Definition is: IoC is a property of your program's execution flow, when you don't do anything until they ask you to do it.

    It sounds exactly as "Hollywood Principle", but I believe that "Hollywood Principle" and IoC are both absolutely the same idea.

Do I understand it?

解决方案

My take on it is this: DIP is the principle that guides us towards DI. Basically, loose coupling is the goal, and there are at least two ways to achieve it.

  • Dependency Injection
  • Service Locator

However, Service Locator is an anti-pattern and has nothing to do with the DIP. DI, however, is the correct application of the DIP.

The relationship between DI and IoC has been explained before.

BTW, when talking about DI and loose coupling, I find the terminology laid out in Domain-Driven Design the most applicable. Basically, Services are the only kinds of objects subjected to DI.

这篇关于DIP与DI与国际奥委会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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