您的位置:首页 > 其它

设计模式:简单工厂模式和工厂方法模式的区别

2016-04-22 08:32 369 查看
简单工厂模式:本着高内聚低耦合的原则,将系统的逻辑部分和功能分开
例如简易计算器的实现代码

using System;
using System.Collections.Generic;
using System.Text;

namespace Operation
{
/// <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;
}

}

/// <summary>
/// 加法类
/// </summary>
class OperationAdd : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}

/// <summary>
/// 减法类
/// </summary>
class OperationSub : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}

/// <summary>
/// 乘法类
/// </summary>
class OperationMul : Operation
{
public override double getResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}

/// <summary>
/// 除法类
/// </summary>
class OperationDiv : Operation
{
public override double getResult()
{
double result = 0;
if (NumberB==0)
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}

/// <summary>
/// 平方类
/// </summary>
class OperationSqr : Operation
{
public override double getResult()
{
double result = 0;
result = NumberB * NumberB;
return result;
}
}

/// <summary>
/// 平方根类
/// </summary>
class OperationSqrt : Operation
{
public override double getResult()
{
double result = 0;
if (NumberB < 0)
throw new Exception("负数不能开平方根。");
result = Math.Sqrt(NumberB);
return result;
}
}

/// <summary>
/// 相反数类
/// </summary>
class OperationReverse : Operation
{
public override double getResult()
{
double result = 0;
result = -NumberB;
return result;
}
}

/// <summary>
/// 运算类工厂
/// </summary>
class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
{
oper = new OperationAdd();
break;
}
case "-":
{
oper = new OperationSub();
break;
}
case "*":
{
oper = new OperationMul();
break;
}
case "/":
{
oper = new OperationDiv();
break;
}
case "sqr":
{
oper = new OperationSqr();
break;
}
case "sqrt":
{
oper = new OperationSqrt();
break;
}
case "+/-":
{
oper = new OperationReverse();
break;
}
}

return oper;
}
}
}


 

工厂方法模式:和工厂方法不同的地方是加入了“开放-封闭原则”(软件实体类、模块或者函数等等,应该可以扩展,但是不可以修改)规则,将简单工厂的内部判断逻辑移动到了客户端代码来进行,在扩展新功能的时候,简单工厂模式要修改工厂类,工厂方法模式是修改客户端。

计算器实现代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace 工厂方法_计算器
{
/// <summary>
/// 运算类
/// </summary>
class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
/// <summary>
/// 得到运算结果
/// </summary>
/// <returns></returns>
public virtual double GetResult()
{
double result = 0;
return result;
}
}

/// <summary>
/// 加法类
/// </summary>
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}

/// <summary>
/// 减法类
/// </summary>
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
/// <summary>
/// 乘法类
/// </summary>
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
/// <summary>
/// 除法类
/// </summary>
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}

/// <summary>
/// 工厂方法
/// </summary>
interface IFactory
{
Operation CreateOperation();
}

/// <summary>
/// 专门负责生产“+”的工厂
/// </summary>
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}

/// <summary>
/// 专门负责生产“-”的工厂
/// </summary>
class SubFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}

/// <summary>
/// 专门负责生产“*”的工厂
/// </summary>
class MulFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationMul();
}
}

/// <summary>
/// 专门负责生产“/”的工厂
/// </summary>
class DivFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
}


 

参考资料:程杰《大话设计模式》

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: