C# 依赖注入那些事儿

时间:2023-10-01 19:50:08

原文地址: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[].ChildNodes[].ChildNodes[]; 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();
}
}
}