Dependency Injection

Dependency Injection

        IoC模式基本上是一个高层的概念,在 Martin Fowler 的
Inversion of Control Containers and the Dependency Injection 中谈到,实现IoC有两种方式: Dependency Injection与Service Locator, Castle 所采用的是Dependency Injection 来实现 IoC,中文翻译为依赖注入,依赖注入的意义是:“保留抽象接口,让组件依赖于抽象接口,当组件要与其它实际的对象发生依赖关系时,藉过抽象接口来注入依赖的实际对象。”

看看下面这个程序:

Dependency Injection
Dependency Injection
public
 
class
 BusinessObject 
Dependency Injection

Dependency Injection    
private FloppyWriter writer = new FloppyWriter(); 
Dependency Injection    Dependency Injection
Dependency Injection    
Dependency InjectionDependency Injection    
public void Save() Dependency Injection
Dependency Injection        Dependency Injection 
Dependency Injection        writer.SaveToFloppy(); 
Dependency Injection
Dependency Injection    }
 
Dependency Injection}

 
Dependency Injection
Dependency Injection
Dependency Injection

BusinessObject 依赖于实际的 FloppyWriter,为了让 BusinessObject 获得重用性,不让 BusinessObject 直接依赖于实际的 FloppyWriter,而是依赖于抽象的接口:


Dependency Injection
Dependency Injection
public
 
interface
 IDeviceWriter 
Dependency Injection

Dependency Injection    
public void SaveToDevice(); 
Dependency Injection}

 
Dependency Injection
Dependency InjectionDependency Injection

public
 
class
 BusinessObject 
Dependency Injection

Dependency Injection    
private IDeviceWriter writer; 
Dependency Injection
Dependency Injection    
public DeviceWriter
Dependency InjectionDependency Injection    
Dependency Injection{

Dependency Injection      Set
Dependency InjectionDependency Injection      
Dependency Injection{

Dependency Injection        
this.writer = value; 
Dependency Injection      }

Dependency Injection    }
 
Dependency Injection
Dependency InjectionDependency Injection    
public void Save() Dependency Injection
Dependency Injection        Dependency Injection
Dependency Injection        writer.SaveToDevice(); 
Dependency Injection    }
 
Dependency Injection}

 
Dependency Injection
Dependency InjectionDependency Injection

public
 
class
 FloppyWriter : IDeviceWriter 
Dependency Injection

Dependency InjectionDependency Injection    
public void SaveToDevice() Dependency Injection
Dependency Injection        Dependency Injection
Dependency Injection        
// 实现储存至Floppy的程序代码 
Dependency Injection
    }
 
Dependency Injection}

 
Dependency Injection
Dependency InjectionDependency Injection

public
 
class
 UsbDiskWriter : IDeviceWriter 
Dependency Injection

Dependency InjectionDependency Injection    
public void SaveToDevice() Dependency Injection
Dependency Injection        Dependency Injection
Dependency Injection        
// 实现储存至UsbDisk的程序代码 
Dependency Injection
    }
 
Dependency Injection}

 
Dependency Injection

如果今天BusinessObject想要与UseDiskWriter对象发生依赖关系,可以这么建立: businessObject.SetDeviceWriter(new UsbDiskWriter());

由于BusinessObject依赖于抽象接口,在需要建立依赖关系时,可以通过抽象接口注入依赖的实际对象。

依赖注入在Martin Fowler的文章中谈到了三种实现方式:Interface injection、Setter injection 与 Constructor injection。并分别称其为Type 1 IoC、Type 2 IoC 与 Type 3 IoC。

上面的BusinessObject所实现的是Type 2 IoC,通过属性注入依赖关系,而Type 3 IoC,则在是构造函数上注入依赖关系,例如:


Dependency Injection
Dependency Injection
public
 
class
 BusinessObject 
Dependency Injection

Dependency Injection    
private IDeviceWriter writer; 
Dependency Injection
Dependency InjectionDependency Injection    
public BusinessObject(IDeviceWriter writer) Dependency Injection
Dependency Injection        
this.writer = writer; 
Dependency Injection    }
 
Dependency Injection
Dependency InjectionDependency Injection    
public void Save() Dependency Injection
Dependency Injection        Dependency Injection
Dependency Injection        writer.SaveToDevice(); 
Dependency Injection    }
 
Dependency Injection}

 
Dependency Injection
Dependency Injection

转载于:https://www.cnblogs.com/Jebel/archive/2008/06/13/1219257.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111013.html原文链接:https://javaforall.net

(0)
上一篇 2021年7月27日 下午12:00
下一篇 2021年7月27日 下午1:00


相关推荐

  • java 僵尸危机秘籍_《僵尸危机3》通关秘籍

    java 僵尸危机秘籍_《僵尸危机3》通关秘籍关于武器:1一开始给的手枪,子弹无限。2UZI:冲锋枪,一开始100发子弹,以后会更多3Shotgun:单暴,打小僵尸一下就死,BOSS5、6下,以后是连暴4Barrel:油桶,放了以后,打一枪就炸,注意别把自己炸死5Grenade:手榴弹,一扔就OK6walls:是墙,用来堵路的7Claymore:炸弹,放一个,走远点,僵尸走到那就爆了8Rocket:火箭弹:离远点再打,容易自残…

    2025年8月1日
    5
  • 留言板的代码_留言板留言大全短句

    留言板的代码_留言板留言大全短句<!DOCTYPEhtml><htmllang=”en”><head><metacharset=”UTF-8″><metahttp-equiv=”X-UA-Compatible”content=”IE=edge”><metaname=”viewport”content=”width=device-width,initial-scale=1.0″><title>D.

    2022年10月21日
    4
  • 完全卸载flash浏览器插件_浏览器内置flash卸载

    完全卸载flash浏览器插件_浏览器内置flash卸载前天晚上不知道点到了什么东西,弹出来个窗口说浏览器的Flash插件需要升级,当时也没多想就确定了,结果发现QQ2009一开就崩溃,囧……找了半天发现问题出在升级的这个Flash插件上面,于是在控制面板中卸载掉,重新安装——一开QQ继续崩溃,继续囧……再查,发现在控制面板中卸载Flash插件之后,在C:\Windows\System32\Macromed\Flash\依旧存在两个文…

    2022年10月15日
    4
  • 聚集索引和非聚集索引的区别 mysql_聚集索引和非聚集索引区别

    聚集索引和非聚集索引的区别 mysql_聚集索引和非聚集索引区别概述 汉语字典的正文本身就是一个聚集索引 比如要查 z 开头的 我们会自然翻到字典的后半部分 而不是从前往后一页一页翻 避免了全表扫描 而当我们翻到那一页 就能得到内容 而不需要再去查其他目录来找到需要找的内容 字典正文内容本身就是一种按照一定规则排列的索引 索引中的叶子节点保存的就是要查找的数据 这种称为聚集索引 如果是用偏旁部首来查 会得到某个字在哪一页 索引中的叶子节点保存的是索引 这种称为非聚

    2026年3月19日
    1
  • 豆包ai怎么增加次数

    豆包ai怎么增加次数

    2026年3月12日
    2
  • PHP进销存erp源码库存管理系统

    PHP进销存erp源码库存管理系统PHP进销存erp源码库存管理系统(2次开发另外收费)本系统开发PHP+MySQL采用CI2.x框架本系统运行环境php5.3+mysql5.5支持IIS、apache不支持nginx源码网站:www.phprr.com演示地址:http://www.phprr.com/show-55账号:admin密码:jxc888888…

    2022年5月22日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号