Spring.NET学习笔记6——依赖注入(应用篇)
1. 谈到高级语言编程,我们就会联想到设计模式;谈到设计模式,我们就会说道怎么样解耦合。而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny Injection)简称DI,目前DI是最优秀的解耦方式之一。下面我就来谈谈依赖注入的应用场景。
我模拟了三种不同的场景,可以一起学习使用依赖注入的重要性。
下面是应用场景的条件:人类使用工具劳动。
一.接口接受但是个直接耦合
2. }
二。工厂解耦 但代码改变的时候工厂里的代码要随着更改 不同业务要new 出许多的工厂仍然是个shit
1. public class Hoe : ITool
}
}
3. public class Spear : ITool }
4. public class Spear : ITool }
5. public class PrimitivePerson : Person
}
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 />
</object>
</objects>
</spring>
</configuration>
7.找个人工作就行了 用什么工作已经看不出了 class Program
}