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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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