原文地址: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();}} }
?