需求:做一个商场收银软件,营业员根据客户所购买商品的单价和数量,向客户收费。
额外需求1:打折(8折,7折,5折不等)
额外需求2:返现(满300返100,满200返50等)
使用简单工厂实现
UML类图
代码
namespace PatternTest.Strategy
{
abstract class CashSuper
{
public abstract double acceptCash(double money);
}
}
namespace PatternTest.Strategy
{
class CashNormal : CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
}
namespace PatternTest.Strategy
{
class CashRebate : CashSuper
{
private double moneyRebate = 1d;
public CashRebate(string moneyRebate)
{
this.moneyRebate = double.Parse(moneyRebate);
}
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
}
namespace PatternTest.Strategy
{
class CashReturn : CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(string moneyCondition, string moneyReturn)
{
this.moneyCondition = double.Parse(moneyCondition);
this.moneyReturn = double.Parse(moneyReturn);
}
public override double acceptCash(double money)
{
double result = money;
if (money >= moneyCondition)
result = money - Math.Floor(money / moneyCondition) * moneyReturn;
return result;
}
}
}
namespace PatternTest.Strategy
{
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("300", "100");
break;
case "打8折":
cs = new CashRebate("0.8");
break;
}
return cs;
}
}
}
//test
double total = 0.0d;
CashSuper cs = CashFactory.createCashAccept("满300返100");
double totalPrices = 0d;
totalPrices = cs.acceptCash(100.00 * 3);
total += totalPrices;
cs = CashFactory.createCashAccept("打8折");
totalPrices = cs.acceptCash(100.00 * 2);
total += totalPrices;
Console.WriteLine("结果是: " + total);
结果
结果是: 360
请按任意键继续. . .
使用策略模式重写
策略模式模型
Strategy:
策略类,定义所有支持的算法公共接口
ConcreteStrategyA、ConcreteStrategyB、ConcreteStrategyC等:
具体的策略类,封装了算法的具体算法或行为,继承于Strategy
Context:
用ConcreteStrategy来配置,具体使用其基类的指针或引用表示
扩展:
可以让Context为基类,扩展其子类,代码更加的灵活
代码:
// Strategy.cs
namespace PatternTest.Strategy
{
abstract class Strategy
{
public abstract void AlgorithmInterface();
}
}
// ConcreteStrategyA.cs
namespace PatternTest.Strategy
{
class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法A实现");
}
}
}
// ConcreteStrategyB.cs
namespace PatternTest.Strategy
{
class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法B实现");
}
}
}
// ConcreteStrategyC.cs
namespace PatternTest.Strategy
{
class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法C实现");
}
}
}
// Context.cs
namespace PatternTest.Strategy
{
class Context
{
Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
public void ContextInterface()
{
strategy.AlgorithmInterface();
}
}
}
//test
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
// 输出结果
算法A实现
算法B实现
算法C实现
请按任意键继续. . .
套用策略模型到简单工厂里
Strategy => CashSuper
ConcreteStrategyA => CashNormal
ConcreteStrategyB => CashRebate
ConcreteStrategyC => CashReturn
Context => CashContext (新增)
UML类图
前面 几个类代码是一样的,只有CashContext.cs是新增的
namespace PatternTest.Strategy
{
class CashContext
{
CashSuper cs = null;
public CashContext(string type)
{
switch (type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("300", "100");
break;
case "打8折":
cs = new CashRebate("0.8");
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
}
double total = 0.0d;
CashContext cc = new CashContext("满300返100");
double totalPrices = 0d;
totalPrices = cc.GetResult(300);
total += totalPrices;
Console.WriteLine("结果是: " + total);
cc = new CashContext("打8折");
totalPrices = cc.GetResult(100.00 * 2);
total += totalPrices;
Console.WriteLine("结果是: " + total);
// 输出结果
结果是: 200
结果是: 360
请按任意键继续. . .
简单工厂模式:客户端会认识两个类CashSuper和CashFactory
策略模式结合简单工厂模式:客户端只认识一个类CashContext
策略模式优点:
1,以相同的方式调用不同的算法,减少了各种算法与使用算法类之间的耦合
2,简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试
缺陷:
CashContext中还是用到了 switch,也就是说如果我们要新增加一个算法,就必须要更改CashContext中的switch分支代码
更好的办法是使用反射