《大話設計模式》中的第一章是一個用簡單原廠模式構建的簡易計算機的例子,在書中的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;
}
}
}
-----------------------------------------Program.cs------------------------------------------using System;using System.Collections.Generic;using System.Text;namespace oper{ class Program { static void Main(string[] args) { Operation oper; while (true) { Console.Write("請輸入數字A:"); string strNumberA = Console.ReadLine(); /// ///選擇運算子可以從設定檔XML讀取 /// Console.Write("請選擇運算子號:/nOperationAdd代表加(+)/nOperationSub代表減(-)/nOperationMul代表乘(*)/nOperationDiv代表除(/)/n"); string strOperate = Console.ReadLine(); oper = OperationFactory.createOperate(strOperate); Console.Write("請輸入數字B:"); string strNumberB = Console.ReadLine(); oper.NumberA = Convert.ToDouble(strNumberA); oper.NumberB = Convert.ToDouble(strNumberB); double result = oper.getResult(); Console.WriteLine("result : " + result); } } }}