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.pswp.cn/news/388902.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388902.shtml
英文地址,請注明出處:http://en.pswp.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