一篇能很好反映普通接口多态 工厂模式 依赖注入三者的耦合强度区别的文章
2011-05-22 13:01
686 查看
转自:http://www.cnblogs.com/GoodHelper/archive/2009/10/26/SpringNET_DI.html
今天看spring.net的文章 这文章的作者用三个很好的例子阐述了依赖注入的解耦性
第一个例子 普通的多态
第二个例子 运用工厂解除依赖耦合
第三个例子 刚是运用了依赖注入彻底解耦 这里它是用了spring.net 实际上一个简单的反射也行啦
第一个例子是最明显的耦合 他没有画图 我给加了张图 vs中自带的类图里怎么也找不着依赖的箭头...我自己画了个箭头 不是很好看...!
/// <summary>
/// 抽象人类
/// </summary>
public abstract class Person
{
/// <summary>
/// 使用工具劳动
/// </summary>
public abstract void Work();
}
public interface ITool
{
/// <summary>
/// 使用工具
/// </summary>
void UseTool();
}
场景一,原始社会:原始人使用长矛打猎
public class Spear : ITool
{
public void UseTool()
{
Console.WriteLine("使用长矛");
}
}
public class PrimitivePerson : Person
{
/// <summary>
/// 原始社会使用长矛打猎
/// </summary>
public override void Work()
{
//知道打猎使用的是长矛,并且制造长矛
ITool tool = new Spear();
tool.UseTool();
Console.WriteLine("使用长矛打猎");
}
}
从上面代码我们不难看出,虽然使用的经典的里氏替换原则,但PrimitivePerson类于Spear类存在着耦合。
第二段代码 运用工厂
可以看到它加了一个工厂 包装了实例化 使得PrimitivePerson与Hoe没有直接存在耦合 但是工厂与PrimitivePerson之间还是有关系的 而且这种简单工厂往往会违背开闭原则(如有错误 求拍砖!)
场景二,经济社会:使用工具耕作
public class Hoe : ITool
{
public void UseTool()
{
Console.WriteLine("使用锄头");
}
}
public static class ToolFactory
{
/// <summary>
/// 工厂制造工具
/// </summary>
/// <returns></returns>
public static ITool CreateTool()
{
return new Hoe(); // 制造锄头
}
}
public class EconomyPerson : Person
{
/// <summary>
/// 经济社会使用锄头耕作
/// </summary>
public override void Work()
{
//不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
ITool tool = ToolFactory.CreateTool();
tool.UseTool();
Console.WriteLine("经济社会使用工具耕作");
}
}
第三段 运用依赖注入
public class Computer : ITool
{
public void UseTool()
{
Console.WriteLine("使用电脑");
}
}
public class ModernPerson : Person
{
/// <summary>
/// 从外部获取工具
/// </summary>
public ITool Tool { get; set; }
/// <summary>
/// 现在人用不需要知道电脑是哪来的,直接拿来办公
/// </summary>
public override void Work()
{
//不知道使用什么工具和哪来的工具,只是机械化的办公
Tool.UseTool();
Console.WriteLine("使用工具办公");
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<description>一个简单的控制反转例子</description>
<object id="computer" type="SpringNetIoC.Computer, SpringNetIoC" />
<object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
<property name="Tool" ref="computer"/>
</object>
</objects>
</spring>
</configuration>
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
Person person = (Person)ctx.GetObject("modernPerson");
person.Work();
Console.ReadLine();
}
}
从上面代码我们可以看出,把对象交给Spring.NET容器进行管理,ModernPerson类不需要知道具体使用什么工具,仅仅是机械化的工作。至于使用的什么工具,则由配置文件决定,所有对象由Spring.NET容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。
今天看spring.net的文章 这文章的作者用三个很好的例子阐述了依赖注入的解耦性
第一个例子 普通的多态
第二个例子 运用工厂解除依赖耦合
第三个例子 刚是运用了依赖注入彻底解耦 这里它是用了spring.net 实际上一个简单的反射也行啦
第一个例子是最明显的耦合 他没有画图 我给加了张图 vs中自带的类图里怎么也找不着依赖的箭头...我自己画了个箭头 不是很好看...!
/// <summary>
/// 抽象人类
/// </summary>
public abstract class Person
{
/// <summary>
/// 使用工具劳动
/// </summary>
public abstract void Work();
}
public interface ITool
{
/// <summary>
/// 使用工具
/// </summary>
void UseTool();
}
场景一,原始社会:原始人使用长矛打猎
public class Spear : ITool
{
public void UseTool()
{
Console.WriteLine("使用长矛");
}
}
public class PrimitivePerson : Person
{
/// <summary>
/// 原始社会使用长矛打猎
/// </summary>
public override void Work()
{
//知道打猎使用的是长矛,并且制造长矛
ITool tool = new Spear();
tool.UseTool();
Console.WriteLine("使用长矛打猎");
}
}
从上面代码我们不难看出,虽然使用的经典的里氏替换原则,但PrimitivePerson类于Spear类存在着耦合。
第二段代码 运用工厂
可以看到它加了一个工厂 包装了实例化 使得PrimitivePerson与Hoe没有直接存在耦合 但是工厂与PrimitivePerson之间还是有关系的 而且这种简单工厂往往会违背开闭原则(如有错误 求拍砖!)
场景二,经济社会:使用工具耕作
public class Hoe : ITool
{
public void UseTool()
{
Console.WriteLine("使用锄头");
}
}
public static class ToolFactory
{
/// <summary>
/// 工厂制造工具
/// </summary>
/// <returns></returns>
public static ITool CreateTool()
{
return new Hoe(); // 制造锄头
}
}
public class EconomyPerson : Person
{
/// <summary>
/// 经济社会使用锄头耕作
/// </summary>
public override void Work()
{
//不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
ITool tool = ToolFactory.CreateTool();
tool.UseTool();
Console.WriteLine("经济社会使用工具耕作");
}
}
第三段 运用依赖注入
public class Computer : ITool
{
public void UseTool()
{
Console.WriteLine("使用电脑");
}
}
public class ModernPerson : Person
{
/// <summary>
/// 从外部获取工具
/// </summary>
public ITool Tool { get; set; }
/// <summary>
/// 现在人用不需要知道电脑是哪来的,直接拿来办公
/// </summary>
public override void Work()
{
//不知道使用什么工具和哪来的工具,只是机械化的办公
Tool.UseTool();
Console.WriteLine("使用工具办公");
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<description>一个简单的控制反转例子</description>
<object id="computer" type="SpringNetIoC.Computer, SpringNetIoC" />
<object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
<property name="Tool" ref="computer"/>
</object>
</objects>
</spring>
</configuration>
class Program
{
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
Person person = (Person)ctx.GetObject("modernPerson");
person.Work();
Console.ReadLine();
}
}
从上面代码我们可以看出,把对象交给Spring.NET容器进行管理,ModernPerson类不需要知道具体使用什么工具,仅仅是机械化的工作。至于使用的什么工具,则由配置文件决定,所有对象由Spring.NET容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。
相关文章推荐
- 一篇能很好反映普通接口多态 工厂模式 依赖注入三者的耦合强度区别的文章
- 普通接口、多态、工厂模式、依赖注入三者的耦合强度区别
- 抽象类和接口的区别(里面有适配器设计、工厂设计、代理设计模式)
- 一篇关于spring ioc 依赖注入3种方式的文章引用
- 今天上午看到一篇非常好的,介绍 抽象与接口区别的文章,特别收藏
- 分层,工厂模式,依赖注入控制反转
- 简单工厂模式和抽象工厂模式的区别:面向接口编程
- 今天上午看到一篇非常好的,介绍 抽象与接口区别的文章,特别收藏
- 模拟实现Struts拦截器-蕴含着代理模式,AOP,工厂模式,依赖注入,Java 反射,动态构造等机制
- 一篇文章搞定Java工厂模式
- Java开发中的23种设计模式,很好的一篇文章
- 对初学java通信很好的一篇文章:TCP/IP协议与UDP协议的区别和认识
- 接口多态静态工厂设计模式模板设计模式
- 一篇关于 依赖倒置、控制反转和依赖注入辨析 的文章
- 分层、工厂模式、依赖注入(控制反转)
- 推荐一篇java抽象类和接口区别的文章
- 工厂方法模式与IoC/DI控制反转和依赖注入
- python里面的全局变量和局部变量的区别(很好的一篇文章,找不到出处,原作者看到请联系,我会署上名字)
- 转载一篇关于 接口和抽象类 的文章,写的很好
- 学习依赖注入模式的2个比较好的文章参考