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

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

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

一.接口接受但是个直接耦合

2.
    /// <summary>
    /// 抽象人类
    /// </summary>
    public abstract class Person
    {
        /// <summary>
        /// 使用工具劳动
        /// </summary>
        public abstract void Work();
    }

二。工厂解耦 但代码改变的时候工厂里的代码要随着更改 不同业务要new 出许多的工厂仍然是个shit

1.
    public class Hoe : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用锄头");
        }
    }

2. public static class ToolFactory
    {
        /// <summary>
        /// 工厂制造工具
        /// </summary>
        /// <returns></returns>
        public static ITool CreateTool()
        {
            return new Hoe();  // 制造锄头
        }
    }
3.
    public class EconomyPerson : Person
    {
        /// <summary>
        /// 经济社会使用锄头耕作
        /// </summary>
        public override void Work()
        {
            //不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
            ITool tool = ToolFactory.CreateTool();
            tool.UseTool();
            Console.WriteLine("经济社会使用工具耕作");
        }
    }
三.彻底解耦
1.   public class Computer : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用电脑");
        }
    }

2.直接属性注入连工具是什么都不用知道 只提供接口 
public class ModernPerson : Person
    {
        /// <summary>
        /// 从外部获取工具
        /// </summary>
        public ITool Tool { get; set; }

        /// <summary>
        /// 现在人用不需要知道电脑是哪来的,直接拿来办公
        /// </summary>
        public override void Work()
        {
            //不知道使用什么工具和哪来的工具,只是机械化的办公
            Tool.UseTool();
            Console.WriteLine("使用工具办公");
        }
    }


    public interface ITool
    {
        /// <summary>
        /// 使用工具
        /// </summary>
        void UseTool();
    }

3. public class Spear : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用长矛");
        }
    }

4.  public class Spear : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用长矛");
        }
    }

5.
    public class PrimitivePerson : Person
    {
        /// <summary>
        /// 原始社会使用长矛打猎
        /// </summary>
        public override void Work()
        {
            //知道打猎使用的是长矛,并且制造长矛
            ITool tool = new Spear(); 客户端与spear直接耦合就是个shit
            tool.UseTool();
            Console.WriteLine("使用长矛打猎");
        }
    }

6.使用配置文件解耦高级多了

<?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>

7.找个人工作就行了 用什么工作已经看不出了
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            Person person = (Person)ctx.GetObject("modernPerson");
            person.Work();

            Console.ReadLine();
        }
    }

最新文章

  1. 作业三: 代码规范、代码复审、PSP
  2. Day3-python基础3
  3. 在Win8中用批处理创建Oracle数据库时报“Unable to open file”
  4. linux笔记一
  5. 使用JavaScript+Html创建win8应用(二)
  6. C#加密NodeJS解密
  7. C++:类的组合
  8. nrpe 在ubuntu上安装遇到的问题
  9. oracle删除表的方法
  10. OC加强-day04
  11. C#调用百度云存储接口上传文件
  12. CachedRowSet使用
  13. Linux下进程间通信的六种机制详解
  14. Jenkins持续集成演示
  15. property相关补充
  16. mysql 开发进阶篇系列 19 MySQL Server(innodb_flush_log_at_trx_commit与sync_binlog)
  17. 深入理解Java中的同步静态方法和synchronized(class)代码块的类锁
  18. 2.Yum仓库优化
  19. @Entity 和 @Table
  20. tensorflow 高级api使用分布式之配置

热门文章

  1. (19/24) webpack实战技巧:推荐使用的第三方类库打包方法
  2. Simple2D-25 精灵动作
  3. Java多线程对同一个对象进行操作
  4. Unified shader model
  5. 分页导航jsp
  6. Openning SharePoint - 80 website gives HTTP 404 Error, The webpage cannot be found ! on SharePoint 2013
  7. update from
  8. MySql log_bin
  9. RWIGS and LORBIT (1)
  10. Python globals() 函数