反射使抽象工厂模式的面向对象更上一层楼(知识点:依赖注入,反射,多态性,操作XML文件等)
时间:2011-06-09 来源:Lose.zhang
实例文件:
namespace test
{
internal static class ReflectionFactory
{
private static String _windowType;
private static String _buttonType;
private static String _textBoxType;
static ReflectionFactory()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"f:/test.xml");
XmlNode xmlNode = xmlDoc.ChildNodes[1];
_windowType = xmlNode.ChildNodes[0].ChildNodes[0].Value;
_buttonType = xmlNode.ChildNodes[1].ChildNodes[0].Value;
_textBoxType = xmlNode.ChildNodes[2].ChildNodes[0].Value;
}
public static IWindow MakeWindow()
{
return Assembly.Load("test").CreateInstance("test." + _windowType) as IWindow;
}
public static IButton MakeButton()
{
return Assembly.Load("test").CreateInstance("test." + _buttonType) as IButton;
}
public static ITextBox MakeTextBox()
{
return Assembly.Load("test").CreateInstance("test." + _textBoxType) as ITextBox;
}
}
internal interface IButton
{
String ShowInfo();
}
internal interface IWindow
{
String ShowInfo();
}
internal interface ITextBox
{
String ShowInfo();
}
#region 按钮
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;
}
}
#endregion
#region 窗口
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;
}
}
#endregion
#region 文本框
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;
}
}
#endregion
internal interface IFactory
{
IWindow MakeWindow();
IButton MakeButton();
ITextBox MakeTextBox();
}
internal sealed class WindowsFactory : IFactory
{
public IWindow MakeWindow()
{
return new WindowsWindow();
}
public IButton MakeButton()
{
return new WindowsButton();
}
public ITextBox MakeTextBox()
{
return new WindowsTextBox();
}
}
internal sealed class MacFactory : IFactory
{
public IWindow MakeWindow()
{
return new MacWindow();
}
public IButton MakeButton()
{
return new MacButton();
}
public ITextBox MakeTextBox()
{
return new MacTextBox();
}
}
}
调用代码:
#region 通过学习接口与反射再次看看抽象工厂模式
IWindow window = ReflectionFactory.MakeWindow();
Console.WriteLine("创建 " + window.ShowInfo());
IButton button = ReflectionFactory.MakeButton();
Console.WriteLine("创建 " + button.ShowInfo());
ITextBox textBox = ReflectionFactory.MakeTextBox();
Console.WriteLine("创建 " + textBox.ShowInfo());
#endregion
XML文件代码:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<window>MacWindow</window>
<button>MacButton</button>
<textBox>MacTextBox</textBox>
</config>
对于依赖注入DI,平时我们项目中用的最多的就是构造依赖注入技术,形式如:
/// <summary>
/// 构造依赖注入
/// </summary>
IRepository _repository;
public DepartmentsRepository(IRepository repository)
{
_repository = repository;
}
在建立它实例时,为它的依赖注入点传入一个IRepository 实现的类对象即可.这实现上也体现了
面向对象的多态性.
相关阅读 更多 +