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[1].ChildNodes[0].ChildNodes[0];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();}}
}
View Code

 

转载于:https://www.cnblogs.com/guxingy/p/10114352.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388902.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

asp.net core Serilog的使用

先贴上关于使用这个日志组件的一些使用方法&#xff0c;等有时间了在吧官方的文档翻译一下吧&#xff0c;现在真是没时间。 Serilog在使用上主要分为两大块&#xff1a; 第一块是主库&#xff0c;包括Serilog以及Serilog.AspNetCore&#xff0c;如果导入后一个的话会自动导入前…

在FAANG面试中破解堆算法

In FAANG company interview, Candidates always come across heap problems. There is one question they do like to ask — Top K. Because these companies have a huge dataset and they can’t always go through all the data. Finding tope data is always a good opti…

android webView的缓存机制和资源预加载

android 原生使用WebView嵌入H5页面 Hybrid开发 一、性能问题 android webview 里H5加载速度慢网络流量大 1、H5页面加载速度慢 渲染速度慢 js解析效率 js本身的解析过程复杂、解析速度不快&#xff0c;前端页面设计较多的js代码文件 手机硬件设备的性能 机型多&#xff0c;…

mysql springboot 缓存_Spring Boot 整合 Redis 实现缓存操作

摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载&#xff0c;保留摘要&#xff0c;谢谢&#xff01;『 产品没有价值&#xff0c;开发团队再优秀也无济于事 – 《启示录》 』本文提纲一、缓存的应用场景二、更新缓存的策略三、运行 springboot-mybatis-redis 工程…

http压力测试工具及使用说明

http压力测试工具及使用说明 转 说明&#xff1a;介绍几款简单、易使用http压测工具&#xff0c;便于研发同学&#xff0c;压测服务&#xff0c;明确服务临界值&#xff0c;寻找服务瓶颈点。 压测时候可重点以下指标&#xff0c;关注并发用户数、TPS&#xff08;每秒事务数量&a…

itchat 道歉_人类的“道歉”

itchat 道歉When cookies were the progeny of “magic cookies”, they were seemingly innocuous packets of e-commerce data that stored a user’s partial transaction state on their computer. It wasn’t disclosed that you were playing a beneficial part in a muc…

使用Kubespray部署生产可用的Kubernetes集群(1.11.2)

Kubernetes的安装部署是难中之难&#xff0c;每个版本安装方式都略有区别。笔者一直想找一种支持多平台 、相对简单 、适用于生产环境 的部署方案。经过一段时间的调研&#xff0c;有如下几种解决方案进入笔者视野&#xff1a; 部署方案优点缺点Kubeadm官方出品部署较麻烦、不够…

android webView 与 JS交互方式

webView 与JS交互 Android调用JS代码的方法有&#xff1a; 通过WebView的loadUrl&#xff08;&#xff09;通过WebView的evaluateJavascript&#xff08;&#xff09; 对于JS调用Android代码的方法有3种&#xff1a; 通过WebView的addJavascriptInterface&#xff08;&…

matlab软件imag函数_「复变函数与积分变换」基本计算代码

使用了Matlab代码&#xff0c;化简平时遇到的计算问题&#xff0c;也可以用于验算结果来自211工科专业2学分复变函数与积分变换课程求复角主值sym(angle(待求复数))%公式 sym(angle(1sqrt(3)*i))%举例代入化简将 代入关于z的函数f(z)中并化解&#xff0c;用于公式法计算无穷远点…

数据科学 python_为什么需要以数据科学家的身份学习Python的7大理由

数据科学 pythonAs a new Data Scientist, you know that your path begins with programming languages you need to learn. Among all languages that you can select from Python is the most popular language for all Data Scientists. In this article, I will cover 7 r…

[luoguP4142]洞穴遇险

https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西&#xff0c;要不就是轮廓线\(DP\)&#xff0c;要不就是网络流。 为了表示奇数点&#xff08;即\((xy)\%21\)&#xff09;的危险值&#xff0c;把该点拆为两个点&#xff0c;连一条边长…

飞信虚拟机

做完了一个图片处理软件,突然想到上次上网看到C#程序脱离.NET FRAMEWORK运行的文章,于是决定自己动手试一下。 之前看到有用别的方法来实现的&#xff0c;但我还是选择了现在比较流行的软件飞信中带的VMDotNet&#xff0c;也就是所谓的.NET FRAMEWORK虚拟机吧。相信有很多人也已…

django的contenttype表

https://blog.csdn.net/aaronthon/article/details/81714496 这篇文章已经非常详细了,供自己以后忘了...回看...... 总结&#xff1a; 当一张表和多个表FK关联&#xff0c;并且多个FK中只能选择其中一个或其中n个时&#xff0c;可以利用contenttype&#xff0c;固定用三个字段…

视频播放问题和提高性能方案

1.Five symptoms of poor video performance 1.1 视频加载缓慢 ​Perceived Wait Time Time to first frame (TTFF): ​ 播放开始所需的adaptive bitrate(ABR)流媒体段的数量。(我们稍后将对此进行更详细的讨论。) ​ 视频请求发送到视频加载之间的时间(即接收到足够的数据…

rabbitmq 不同的消费者消费同一个队列_RabbitMQ 消费端限流、TTL、死信队列

消费端限流1. 为什么要对消费端限流假设一个场景&#xff0c;首先&#xff0c;我们 Rabbitmq 服务器积压了有上万条未处理的消息&#xff0c;我们随便打开一个消费者客户端&#xff0c;会出现这样情况: 巨量的消息瞬间全部推送过来&#xff0c;但是我们单个客户端无法同时处理这…

动量策略 python_在Python中使用动量通道进行交易

动量策略 pythonMost traders use Bollinger Bands. However, price is not normally distributed. That’s why only 42% of prices will close within one standard deviation. Please go ahead and read this article. However, I have some good news.大多数交易者使用布林…

css3 变换、过渡效果、动画

1 CSS3 选择器 1.1 基本选择器 1.2 层级 空格 > .itemli ~ .item~p 1.3 属性选择器 [attr] [attrvalue] [attr^value] [attr$value] [attr*value] [][][] 1.4 伪类选择器 :link :visited :hover :active :focus :first-child .list li:first-child :last-chi…

webservice 启用代理服务器

您会发现你写完了一个webservice在调用的时候发现怎也没办法调用&#xff0c;一个简单的webservice怎么不能使用&#xff0c;一肚子的怨恨&#xff0c;哈哈您可能没有为webservice设置代理。 下面就给您写个调用的用例和大家分享下。其实很简单&#xff0c;但是你没有想到的时…

mysql常用的存储引擎_Mysql存储引擎

什么是存储引擎&#xff1f;关系数据库表是用于存储和组织信息的数据结构&#xff0c;可以将表理解为由行和列组成的表格&#xff0c;类似于Excel的电子表格的形式。有的表简单&#xff0c;有的表复杂&#xff0c;有的表根本不用来存储任何长期的数据&#xff0c;有的表读取时非…

android studio设计模式和文本模式切换

转载于:https://www.cnblogs.com/judes/p/9437104.html