日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 依赖注入那些事儿

發布時間:2023/11/29 C# 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的C# 依赖注入那些事儿的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。