优化后《简单工厂模式》构造的计算器代码—C#“反射”技术(dll)
来源:互联网 发布:php产品授权系统 编辑:程序博客网 时间:2024/05/22 14:53
《大话设计模式》中的第一章是一个用简单工厂模式构建的简易计算器的例子,在书中的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;
}
}
}
- 优化后《简单工厂模式》构造的计算器代码—C#“反射”技术(dll)
- 优化后《简单工厂模式》构造的计算器代码—C#“反射”技术
- 改良用简单工厂模式构造的计算器代码—“反射”技术
- 改良用简单工厂模式构造的计算器代码—“反射”技术
- 改良用简单工厂模式构造的计算器代码—“反射”技术
- 简单工厂模式的计算器(C++)
- 【设计模式】工厂模式结合反射技术的简单应用
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- 计算器 --- 简单工厂模式
- 简单工厂模式-计算器
- 简单工厂模式计算器
- 简单工厂模式-计算器
- 简单工厂模式---计算器
- 简单工厂模式-计算器
- Windows Media Related
- 航天器健康管理技术研究综述
- 神经网络编程入门
- JAVAP -C反汇编指令的学习(转载)
- 机器学习中的相似性度量
- 优化后《简单工厂模式》构造的计算器代码—C#“反射”技术(dll)
- IO
- 横向扩展(Scale-Out)存储
- excel数值类型转换为文本
- 一些linux资源
- 时间序列识别与预测语句
- freshmen
- 关于SNTP广播模式的疑惑
- skyeye1.3.2安装过程记录