Spring.NET学习笔记6——依赖注入(应用篇) Level 300

  谈到高级语言编程,我们就会联想到设计模式;谈到设计模式,我们就会说道怎么样解耦合。而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny Injection)简称DI,目前DI是最优秀的解耦方式之一。下面我就来谈谈依赖注入的应用场景。

  我模拟了三种不同的场景,可以一起学习使用依赖注入的重要性。

  下面是应用场景的条件:人类使用工具劳动。

ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
    
/// 抽象人类
    
/// </summary>

    public abstract class Person
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 使用工具劳动
        
/// </summary>

        public abstract void Work();
    }


    
public interface ITool
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 使用工具
        
/// </summary>

        void UseTool();
    }

 

  场景一,原始社会:原始人使用长矛打猎

     public   class  Spear : ITool
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public void UseTool()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"使用长矛");
        }

    }
ContractedBlock.gif ExpandedBlockStart.gif PrimitivePerson
    public class PrimitivePerson : Person
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 原始社会使用长矛打猎
        
/// </summary>

        public override void Work()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//知道打猎使用的是长矛,并且制造长矛
            ITool tool = new Spear();
            tool.UseTool();
            Console.WriteLine(
"使用长矛打猎");
        }

    }

从上面代码我们不难看出,虽然使用的经典的里氏替换原则,但PrimitivePerson类于Spear类存在着耦合。

  

  场景二,经济社会:使用工具耕作

     public   class  Hoe : ITool
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public void UseTool()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"使用锄头");
        }

    }
ContractedBlock.gif ExpandedBlockStart.gif ToolFactory
    public static class ToolFactory
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 工厂制造工具
        
/// </summary>
        
/// <returns></returns>

        public static ITool CreateTool()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new Hoe();  // 制造锄头
        }

    }

 

ContractedBlock.gif ExpandedBlockStart.gif EconomyPerson
    public class EconomyPerson : Person
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 经济社会使用锄头耕作
        
/// </summary>

        public override void Work()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
            ITool tool = ToolFactory.CreateTool();
            tool.UseTool();
            Console.WriteLine(
"经济社会使用工具耕作");
        }

    }

 

 

 

从上面代码我可以看出:运用的经典的工厂模式, EconomyPerson仅仅对工厂耦合,并不关心工厂是怎样制造工具。

  

  场景三,现在社会:使用工具办公

     public   class  Computer : ITool
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public void UseTool()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Console.WriteLine(
"使用电脑");
        }

    }

 

ContractedBlock.gif ExpandedBlockStart.gif ModernPerson
    public class ModernPerson : Person
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 从外部获取工具
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public ITool Tool getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 现在人用不需要知道电脑是哪来的,直接拿来办公
        
/// </summary>

        public override void Work()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//不知道使用什么工具和哪来的工具,只是机械化的办公
            Tool.UseTool();
            Console.WriteLine(
"使用工具办公");
        }

    }

 

ContractedBlock.gif ExpandedBlockStart.gif App.config
<?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>

 

ContractedBlock.gif ExpandedBlockStart.gif Program
    class Program
ExpandedBlockStart.gifContractedBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IApplicationContext ctx 
= ContextRegistry.GetContext();
            Person person 
= (Person)ctx.GetObject("modernPerson");
            person.Work();

            Console.ReadLine();
        }

    }

从上面代码我们可以看出,把对象交给Spring.NET容器进行管理,ModernPerson类不需要知道具体使用什么工具,仅仅是机械化的工作。至于使用的什么工具,则由配置文件决定,所有对象由Spring.NET容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。

 

代码下载

^-^

 

  返回目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值