`
bollaxu
  • 浏览: 217080 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Dependency Injection 依赖注入

    博客分类:
  • C#
阅读更多

先概括Dependency Injection也就是依赖注入:一种设计模式,当类设计中使用到某种其它的类或插件类时,是通过对象引用而不是直接创建对象来实现的,在声明的时候,只需确定接口或者基类就可以了。

 

引用概念提出者martin fowler的原图,在一种设计模式下,一个类可以在类的定义里面去创建和使用另一个类,例如:

Figure 1

这里MovieLister类创建了MovieFinderImpl类,而MovieFinderImpl类实现了MovieFinder的接口。这种情况下,如果MovieLister如果要使用其他实现MovieFinder接口的类,就不得不改变创建对象的实现。比如,原来是new MovieFinderImpl_1(),现在是new MovieFinderImpl_2()。

而依赖注入的设计方法引入了一个“注入点”的概念,把依赖关系独立到另外的部分,而不是MovieLister本身的实现中来做,MoviewLister这个类不需要关心最终使用的是哪个MovieFinder的实现类。依赖注入的关系图如下:

Figure 2

在上面的图里面,我们可以看到,我们只规定MovieLister依赖于MovieFinder,而具体有哪些对MovieFinder的实现类,我们并不关心。具体的对象创建和传入都是由Assembler来处理的。这样一来,我们可以保证MovieLister本身对于MovieFinder扩展时表现的可扩展性。

 

在广义上,Dependency Injection也是Inversion of Control(IoC)的一种。Dependency Injection在查找/使用插件类的时候,控制反转了,一般的模式(第一幅图)是MovieLister进行控制,而依赖注入模式下,是由Assembler进行控制,相对于MovieLister来说,控制方向变化了。

 

有两篇关于依赖注入的文章写得比较好,一篇是martin fowler的原文,另一篇是中文的,例子写得也比较好。

1. http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html

2. http://www.martinfowler.com/articles/injection.html

 

分享到:
评论

相关推荐

    《Dependency Injection》电子书

    一本专注于依赖注入的技术书籍。 本书由GOOGLE软件工程师编写,不仅介绍了依赖注入相关的技术知识,而且讲解了与之相关的Spring和Guice框架。 适合开始接触和使用DI的技术者以及掌握该技术并拥有相关开发经历的...

    依赖注入:Dependency injection-英文书签文字版

    依赖注入:Dependency injection-英文书签文字版 全面讲解Spring中的依赖注入,是英文的文字版,带书签

    Dependency Injection with Unity (Microsoft patterns & practices)

    The guide you are holding in your hands is a primer on using dependency injection with Unity – a lightweight extensible dependency injection container built by the Microsoft patterns & practices team...

    Dependency Injection In Action

    深入学习依赖注入原理的好书 Dependency Injection is an in-depth guide to the current best practices for using the Dependency Injection pattern-the key concept in Spring and the rapidly-growing Google ...

    Dependency Injection in .NET

    Summary Dependency Injection in .NET presents core DI patterns in plain C#, so you'll fully understand how DI works, covers integration with standard Microsoft technologies like ASP.NET MVC, and ...

    Spring开发指南

    Dependency Injection 依赖注入的几种实现类型 Type1 接口注入 Type2 设值注入 Type3 构造子注入 几种依赖注入模式的对比总结 Spring Bean封装机制 Bean Wrapper Bean Factory ApplicationContext Web ...

    jsp编程技术复习题

    DI(Dependency Injection依赖注入):意思自身对象中的内置对象是通过注入的方式进行创建。 11.Spring AOP的五个通知类型是什么?环绕通知的工作原理? 答:Before、After-returning、After-throwing、Around、...

    SpringSecutiry实现认证授权功能,整合SpringBoot

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    AngularJs Dependency Injection(DI,依赖注入)

    本文主要介绍AngularJs Dependency Injection,这里整理了详细资料及示例代码有兴趣的小伙伴可以参考下

    Dependency Injection SouceCode

    Prasanna的著作《依赖注入》是一本力图详细探究依赖注入领域,并呈现Spring和Guice技术的著作。Dhanji是Google的一名软件工程师,从事Google Wave的研发,并对Guice、MVCL和其它开源项目做出了贡献。

    Spring Security、Spring Social 、Spring Security OAuth

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    [依赖注入] 依赖注入 实战 (英文版)

    [Manning Publications] 依赖注入 实战 (英文版) [Manning Publications] Dependency Injection (E-Book) ☆ 图书概要:☆ Dependency Injection is an in-depth guide to the current best practices for using ...

    dependency-injection-example, 依赖注入简介.zip

    dependency-injection-example, 依赖注入简介 依赖项注入示例依赖注入不一定是复杂的。 让我们一起解决吧 !是什么?依赖注入是一种方便管理类依赖性的技术,并且允许使用模拟技术更好地测试。 在互联网上有很多例子...

    SpringSecurity.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    AngularJS学习第二篇 AngularJS依赖注入

    简介: 首先我们需要理解什么是依赖注入?...DI Dependency Injection 依赖注入:站在应用程序的角度。A依赖B获取C,B将C注入A。即应用程序依赖容器创建并注入它所需要的外部资源。 AngularJS依赖注入 Provi

    PHP依赖倒置(Dependency Injection)代码实例

    主要介绍了PHP依赖倒置(Dependency Injection)代码实例本文只提供实现代码,需要的朋友可以参考下

    Dependency.Injection.in.NET

    .net 依赖注入技术

Global site tag (gtag.js) - Google Analytics