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,一经查实,立即删除!

相关文章

在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…

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

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

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…

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…

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…

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

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

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

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

高斯模糊为什么叫高斯滤波_为什么高斯是所有发行之王?

高斯模糊为什么叫高斯滤波高斯分布及其主要特征&#xff1a; (Gaussian Distribution and its key characteristics:) Gaussian distribution is a continuous probability distribution with symmetrical sides around its center. 高斯分布是连续概率分布&#xff0c;其中心周…

C# webbrowser 代理

百度&#xff0c;google加自己理解后&#xff0c;将所得方法总结一下&#xff1a; 方法1&#xff1a;修改注册表Software//Microsoft//Windows//CurrentVersion//Internet Settings下 ProxyEnable和ProxyServer。这种方法适用于局域网用户&#xff0c;拨号用户无效。 1p…

C MySQL读写分离连接串_Mysql读写分离

一 什么是读写分离MySQL Proxy最强大的一项功能是实现“读写分离(Read/Write Splitting)”。基本的原理是让主数据库处理事务性查询&#xff0c;而从数据库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库。当然&#xff0c;主服务器也可以提供…

从Jupyter Notebook到脚本

16 Aug: My second article: From Scripts To Prediction API8月16日&#xff1a;我的第二篇文章&#xff1a; 从脚本到预测API As advanced beginners, we know quite a lot: EDA, ML concepts, model architectures etc…… We can write a big Jupyter Notebook, click “Re…

加勒比海兔_加勒比海海洋物种趋势

加勒比海兔Ok, here’s a million dollar question: is the Caribbean really dying? Or, more specifically, are marine species found on Caribbean reefs becoming less abundant?好吧&#xff0c;这是一个百万美元的问题&#xff1a;加勒比海真的死了吗&#xff1f; 或者…

tornado 简易教程

引言 回想Django的部署方式 以Django为代表的python web应用部署时采用wsgi协议与服务器对接&#xff08;被服务器托管&#xff09;&#xff0c;而这类服务器通常都是基于多线程的&#xff0c;也就是说每一个网络请求服务器都会有一个对应的线程来用web应用&#xff08;如Djang…

人口密度可视化_使用GeoPandas可视化菲律宾的人口密度

人口密度可视化GeoVisualization /菲律宾。 (GeoVisualization /Philippines.) Population density is a crucial concept in urban planning. Theories on how it affects economic growth are divided. Some claim, as Rappaport does, that an economy is a form of “spati…

Unity - Humanoid设置Bip骨骼导入报错

报错如下&#xff1a; 解决&#xff1a; 原因是biped骨骼必须按照Unity humanoid的要求设置&#xff0c;在max中设置如下&#xff1a; 转载于:https://www.cnblogs.com/CloudLiu/p/10746052.html

Kubernetes - - k8s - v1.12.3 OpenLDAP统一认证

1&#xff0c;基本概念 为了方便管理和集成jenkins&#xff0c;k8s、harbor、jenkins均使用openLDAP统一认证。2&#xff0c;部署openLDAP 根据之前的文档&#xff0c;openLDAP使用GFS进行数据持久化。下载对应的openLDAP文件git clone https://github.com/xiaoqshuo/k8s-clust…

srpg 胜利条件设定_英雄联盟获胜条件

srpg 胜利条件设定介绍 (Introduction) The e-sports community has been growing rapidly in the past few years, and what used to be a casual pastime has morphed into an industry projected to generate $1.8 B in revenue by 2022. While there are many video games …