《大话设计模式》中的第一章是一个用简单工厂模式构建的简易计算器的例子,在书中的P10-P11页中有个工厂类OperaationFactory用来构造各个运算类的实例,但这里有个问题:如果需要增加新的运算类,除了要修改界面的代码,还要在OperaationFactory的switch中增加新的语句!以下用反射很好的解决了这个问题~~~
///以下类编译为 .dll/
-------------------------------------
Operation.cs
-------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
/// <summary>
/// 运算类
/// </summary>
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;
/// <summary>
/// 数字A
/// </summary>
public double NumberA
{
get
{
return _numberA;
}
set
{
_numberA = value;
}
}
/// <summary>
/// 数字B
/// </summary>
public double NumberB
{
get
{
return _numberB;
}
set
{
_numberB = value;
}
}
/// <summary>
/// 得到运算结果
/// </summary>
/// <returns></returns>
public virtual double getResult()
{
double result = 0;
return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
OperationAdd.cs
-----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
/// <summary>
/// 加法类
/// </summary>
class OperationAdd : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
OperationSub.cs
-----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
/// <summary>
/// 减法类
/// </summary>
class OperationSub : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
OperationMul.cs
-----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
/// <summary>
/// 乘法类
/// </summary>
class OperationMul : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
}
-----------------------------------------
**************************
-----------------------------------------
OperationDiv.cs
-----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace oper
{
/// <summary>
/// 除法类
/// </summary>
class OperationDiv : Operation
{
public override double getResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}
}
-----------------------------------------
**************************
///以下类编译为 .dll///结束/
-------------------------------------------
OperationFactory.cs
-------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace oper
{
/// <summary>
/// 运算类工厂
/// </summary>
public class OperationFactory
{
public static Operation createOperate(string dllpath)
{
//operate = "oper." + operate;
Operation oper;
//Object obj = Activator.CreateInstance(Type.GetType(operate));
//oper = (Operation)obj;
Assembly ass = Assembly.LoadFrom(dllpath+".dll");
dllpath="oper." + dllpath;
Type type=ass.GetType(dllpath);//利用类型的命名空间和名称获得类型
Object obj = Activator.CreateInstance(type);//利用指定的参数实例话类型
oper = (Operation)obj;
return oper;
}
}
}