C# 依赖注入那些事儿
原文地址:http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html
里面有一个例子差了些代码,补全后贴上。
3.1.3 依赖获取
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; //定义了三个接口 IWindow IButton ITextBox
namespace DependencyLocate
{
internal interface IWindow
{
String ShowInfo();
}
internal interface IButton
{
String ShowInfo();
}
internal interface ITextBox
{
String ShowInfo();
}
} //实现接口 IWindow, 实现类 WindowsWindow、MacWindow
namespace DependencyLocate
{
internal sealed class WindowsWindow : IWindow
{
public String Description { get; private set; } public WindowsWindow()
{
this.Description = "Windows风格窗体";
} public String ShowInfo()
{
return this.Description;
}
} internal sealed class MacWindow : IWindow
{
public String Description { get; private set; } public MacWindow()
{
this.Description = " Mac风格窗体";
} public String ShowInfo()
{
return this.Description;
}
}
} //实现接口 IButton, 实现类 WindowsButton、MacButton
namespace DependencyLocate
{
internal sealed class WindowsButton : IButton
{
public String Description { get; private set; } public WindowsButton()
{
this.Description = "Windows风格按钮";
} public String ShowInfo()
{
return this.Description;
}
} internal sealed class MacButton : IButton
{
public String Description { get; private set; } public MacButton()
{
this.Description = " Mac风格按钮";
} public String ShowInfo()
{
return this.Description;
}
}
} //实现接口 ITextBox, 实现类 WindowsTextBox、MacTextBox
namespace DependencyLocate
{
internal sealed class WindowsTextBox : ITextBox
{
public String Description { get; private set; } public WindowsTextBox()
{
this.Description = "Windows风格文本框";
} public String ShowInfo()
{
return this.Description;
}
} internal sealed class MacTextBox : ITextBox
{
public String Description { get; private set; } public MacTextBox()
{
this.Description = " Mac风格文本框";
} public String ShowInfo()
{
return this.Description;
}
}
} namespace DependencyLocate
{
internal interface IFactory
{
IWindow MakeWindow(); IButton MakeButton(); ITextBox MakeTextBox();
}
} namespace DependencyLocate
{
internal sealed class WindowsFactory : IFactory
{
public IWindow MakeWindow()
{
return new WindowsWindow();
} public IButton MakeButton()
{
return new WindowsButton();
} public ITextBox MakeTextBox()
{
return new WindowsTextBox();
}
}
} namespace DependencyLocate
{
internal sealed class MacFactory : IFactory
{
public IWindow MakeWindow()
{
return new MacWindow();
} public IButton MakeButton()
{
return new MacButton();
} public ITextBox MakeTextBox()
{
return new MacTextBox();
}
}
} namespace DependencyLocate
{
internal static class FactoryContainer
{
public static IFactory factory { get; private set; } /// <summary>
/// 静态构造函数:
/// 是一个特殊的函数,将在其他所有方法执行之前以及变量或属性被第一次访问之前执行。
/// 这个构造函数是属于类的,而不是属于哪里实例的,就是说这个构造函数只会被执行一次。
/// 也就是在创建第一个实例或引用任何静态成员之前,由.NET自动调用。
/// 可以使用该函数来初始化静态变量,不应该使用实例构造函数初始化静态变量。
/// 地址:https://www.cnblogs.com/aimi/p/5499711.html
/// </summary>
static FactoryContainer()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Config.xml");
XmlNode xmlNode = xmlDoc.ChildNodes[].ChildNodes[].ChildNodes[]; if ("Windows" == xmlNode.Value)
{
factory = new WindowsFactory();
}
else if ("Mac" == xmlNode.Value)
{
factory = new MacFactory();
}
else
{
throw new Exception("Factory Init Error");
}
}
}
} namespace DependencyLocate
{
class Program
{
static void Main(string[] args)
{
IFactory factory = FactoryContainer.factory;
IWindow window = factory.MakeWindow();
Console.WriteLine("创建 " + window.ShowInfo());
IButton button = factory.MakeButton();
Console.WriteLine("创建 " + button.ShowInfo());
ITextBox textBox = factory.MakeTextBox();
Console.WriteLine("创建 " + textBox.ShowInfo()); Console.ReadLine();
}
}
}
C# 依赖注入那些事儿的更多相关文章
- C#中的依赖注入那些事儿
目录 目录 1 IGame游戏公司的故事 1.1 讨论会 1.2 实习生小李的实现方法 1.3 架构师的建议 1.4 小李的小结 2 探究依赖注入 2.1 故事的启迪 2.2 正式定义依赖注入 3 依 ...
- [ASP.NET MVC 小牛之路]04 - 依赖注入(DI)和Ninject
本人博客已转移至:http://www.exblr.com/liam 为什么需要依赖注入 在[ASP.NET MVC 小牛之路]系列的理解MVC模式文章中,我们提到MVC的一个重要特征是关注点分离( ...
- C# 依赖注入
http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 这篇文章真的非常非常好···绝对值得收藏学习. 目录 目录 1 ...
- 依赖注入(DI)和Ninject
[ASP.NET MVC 小牛之路]04 - 依赖注入(DI)和Ninject 本文目录: 1.为什么需要依赖注入 2.什么是依赖注入 3.使用NuGet安装库 4.使用Ninject的一般步骤 5. ...
- c#之依赖注入
C# 依赖注入 http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 1 IGame游戏公司的故事 1.1 讨论会 话说有一个叫 ...
- 依赖注入框架Ninject
为什么需要依赖注入 我们提到MVC的一个重要特征是关注点分离(separation of concerns).我们希望应用程序的各部分组件尽可能多的相互独立.尽可能少的相互依赖. 我们的理想情况是:一 ...
- C#基础知识之依赖注入
目录 1 IGame游戏公司的故事 1.1 讨论会 1.2 实习生小李的实现方法 1.3 架构师的建议 1.4 小李的小结 2 探究依赖注入 2.1 故事的启迪 2.2 正式定义依赖注入 3 依赖注入 ...
- 通过中看不中用的代码分析Ioc容器,依赖注入....
/** * 通过生产拥有超能力的超人实例 来理解IOC容器 */ //超能力模组接口 interface SuperModuleInterface{ public function activate( ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
随机推荐
- Solr Date类型的哪些你不得不了解的细节
我们先来看看Solr日期类型的一些内幕,然后讨论一下Solr日期类型存在的一些问题,最后我们看看怎么解决现存的问题.概述 DateField 在Solr4.x之前,我们只有DateField,这类型现 ...
- Java捕获异常的问题
---恢复内容开始--- 在Java编译过程中,有时候会出现输入未按照规定输入的情况,此时需要警告用户输入错误,这就会是程序运行过程中出现异常.异常就是可预测但是又没办法消除的一种错误.所以在编写过程 ...
- mac中svn服务器的搭建以及如何在eclipse中使用
mac自带了svn客户端和服务端功能. 1.查看svn版本 svnserve --version yintingtingdeMacBook-Pro:~ yintingting$ svnserve -- ...
- [python] 初学python,打卡签到
自学python第一周,学了变量和简单的条件判断. 附上猜数游戏代码 #Author:shijt trueAge=40 count=0 while count<3: guessAge=int(i ...
- Jscraft 使用 Shell 与预先加载别名混合使用
Session session = a.getSessionShell("user", "pwd", "host"); Channel ch ...
- 【Selenium-WebDriver自学】Log4J的设置(十五)
==================================================================================================== ...
- angularjs数据交互
异步问题ajax异步请求数据完数据后给$scope赋值的时候需要检查$scope的数据更新没有.要不然无法绑定数据. <!DOCTYPE html> <html ng-app=&qu ...
- android 开发 写一个RecyclerView布局的聊天室,并且添加RecyclerView的点击事件
实现思维顺序: 1.首先我们需要准备2张.9的png图片(一张图片为左边聊天泡泡,一个图片为右边的聊天泡泡),可以使用draw9patch.bat工具制作,任何图片导入到drawable中. 2.需要 ...
- Patrick Hughes - 错觉3D雕塑艺术
Pictures Patrick Hughes (artist) From Wikipedia, the free encyclopedia Patrick Hughes. Leaning on a ...
- Flex学习笔记-使用MXML和一个AS事件监听器监听事件
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...