在工作中采有良好模式方法,可以提高程序的可读性,可扩展性,而且易于管理。下面就为大家介绍一下抽象工厂。
1基本流程:
实际操作类-------->抽象控制类-------->接口定义类
接口实现类1 接口实现类2
(图片传不上去了,rar文件里面有)
2代码
接口定义类
using
System;
namespace IFactoryList
... {
/**//// <summary>
/// 接口中的方法必须是保护类型,而且继承后必须实现。
/// </summary>
public interface IFactory
...{
void DoOne();
void DoTwo();
void DoThree();
}
}
namespace IFactoryList
... {
/**//// <summary>
/// 接口中的方法必须是保护类型,而且继承后必须实现。
/// </summary>
public interface IFactory
...{
void DoOne();
void DoTwo();
void DoThree();
}
}
抽象制作类
using
System;
using System.Reflection;
namespace IFactoryList
... {
/**//// <summary>
/// FacotryControl 的摘要说明。
/// </summary>
public class FacotryControl
...{
public FacotryControl()
...{
}
public void FactryCreateObject(string typeName)
...{
try
...{
Assembly assembly = Assembly.LoadFrom("IFactoryList.dll");
Type t = assembly.GetType(typeName);
IFactory ifa =null;
ifa = (IFactory)System.Activator.CreateInstance(t);
Console.Read();
}
catch(Exception ex)
...{
Console.WriteLine("类不存在:"+ex.ToString());
}
}
}
}
using System.Reflection;
namespace IFactoryList
... {
/**//// <summary>
/// FacotryControl 的摘要说明。
/// </summary>
public class FacotryControl
...{
public FacotryControl()
...{
}
public void FactryCreateObject(string typeName)
...{
try
...{
Assembly assembly = Assembly.LoadFrom("IFactoryList.dll");
Type t = assembly.GetType(typeName);
IFactory ifa =null;
ifa = (IFactory)System.Activator.CreateInstance(t);
Console.Read();
}
catch(Exception ex)
...{
Console.WriteLine("类不存在:"+ex.ToString());
}
}
}
}
具体实现类
using
System;
namespace IFactoryList
... {
/**//// <summary>
/// DoOne 的摘要说明。
/// </summary>
public class IFactoryDoOne:IFactoryList.IFactory
...{
public IFactoryDoOne()
...{
this.DoOne();
this.DoTwo();
this.DoThree();
}
IFactory 成员#region IFactory 成员
public void DoOne()
...{
Console.WriteLine("one-DoOne");
}
public void DoTwo()
...{
Console.WriteLine("one-DoTwo()");
}
public void DoThree()
...{
Console.WriteLine("one-DoThree()");
}
#endregion
}
}
namespace IFactoryList
... {
/**//// <summary>
/// DoOne 的摘要说明。
/// </summary>
public class IFactoryDoOne:IFactoryList.IFactory
...{
public IFactoryDoOne()
...{
this.DoOne();
this.DoTwo();
this.DoThree();
}
IFactory 成员#region IFactory 成员
public void DoOne()
...{
Console.WriteLine("one-DoOne");
}
public void DoTwo()
...{
Console.WriteLine("one-DoTwo()");
}
public void DoThree()
...{
Console.WriteLine("one-DoThree()");
}
#endregion
}
}
实际操作类
using
System;
using System.Reflection;
using IFactoryList;
namespace IFactoryDemo
... {
/**//// <summary>
///
/// </summary>
class ActionFactory
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
IFactoryList.FacotryControl fc = new FacotryControl();
fc.FactryCreateObject("IFactoryList.IFactoryDoOne");
}
}
}
using System.Reflection;
using IFactoryList;
namespace IFactoryDemo
... {
/**//// <summary>
///
/// </summary>
class ActionFactory
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
IFactoryList.FacotryControl fc = new FacotryControl();
fc.FactryCreateObject("IFactoryList.IFactoryDoOne");
}
}
}
3、关于抽象工厂的实际的操作,还应该配制xml同时操作,具体的情况请大家自己添写。
4、打包下载
5、更多模式尽请期待中(如有不对之处,望指出,谢谢,记得给评论呀)