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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • toast弹窗教程_toast提示是什么

    toast弹窗教程_toast提示是什么2.3.1 反编译APK安装包首先,需要从网上下载样本安装包,这里使用的样本是哒哒文件助手,读者可以从安卓市场下载。在反编译APK之前,还需要从网上下载第三方模拟器,如夜神、雷电等模拟器。安装下载好的样本,运行看一下效果,大概了解一下该APK实现的相关功能,如图2.6所示,该界面就是程序入口界面。                   图2.6…

    2022年9月15日
  • PHP集成环境 Xampp,PHPwamp等等国内外著名的集成环境[通俗易懂]

    PHP集成环境 Xampp,PHPwamp等等国内外著名的集成环境[通俗易懂]原文:http://www.toutiao.com/i6378285337512247809/?tt_from=mobile_qq&utm_campaign=client_share&app=news_article&utm_source=mobile_qq&iid=7663221178&utm_medium=toutiao_android在本地测试网站,有个集成环境直接测试还

    2022年6月28日
  • zookeeper入门教程_kubernetes github

    zookeeper入门教程_kubernetes githubzookeeperwatcher架构zookeeper 配置中心分布式ID分布式锁集群搭建数据一致性协议:zab协议Zookeeper Leader选举Observer角色及其配置watcher架构客户端首先将Watcher注册到服务器,同时将Watch对象保存到客户端的Watch管理器中。当Zookeeper服务器监听到的数据发生变化时,服务器会通知客户端,接着客户端的Watch管理器会触发相关的Watcher来回调响应处理逻辑,从而完成整体的数据发布/订阅流程。javaAPIJava

    2022年8月8日
  • virtualbox usb

    virtualbox usb

    2021年9月15日
  • 华为手机像素密度排行_华为p40pro屏幕像素密度ppi是多少呢

    华为手机像素密度排行_华为p40pro屏幕像素密度ppi是多少呢大家好,我是百科网智能客服时间君,上述问题将由我为大家进行解答。系统版本为EMUI10.1.0.92,华为p40pro屏幕像素密度ppi是441。人眼并不会感觉到明显的屏幕颗粒感,能保证显示的细腻感满足使用要求。屏幕像素密度,即每英寸屏幕所拥有的像素数,英文简称PPI。我们可以这么理解屏幕像素密度,即在一个对角线长度为1英寸的正方形内所拥有的像素数。华为P40Pro是华为公司旗下手机品牌,使用基于…

    2022年6月1日
  • 中国主流的大数据分析厂商

    中国主流的大数据分析厂商随着互联网和IT技术的推进,大数据的应用逐渐渗透到各行各业。尤其是数据体量大的互联网、金融、银行、制造行业。大数据正逐渐改变企业的运营模式,市场导向,进而惠及人们的生活。“大数据”这一概念最早在国外被提及。之后国内外兴起了一系列大数据技术,包括大数据硬件类、大数据分析类、大数据数据处理类等等,也因此诞生了一批大数据厂商。其中最为热门的是大数据分析技术,可以直接应用到企业生产经营,来带直…

    2022年5月17日

发表回复

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

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