策略模式
2020-12-25 09:12:54 7 举报
策略模式
作者其他创作
大纲/内容
//策略模式 public class CashContext -->Context { CashSuper cashSuper = null; public CashContext(string type) { switch (type) { case \"正常收费\": cashSuper = new CashNormal(); break; case \"满300返10\": cashSuper = new CashReturn(\"300\
/// <summary> /// 现金收取抽象类 /// </summary> public abstract class CashSuper --->Strategy { public abstract double acceptCash(double money); }
-Strategy
ConcreteStrategyA
+Algorithmlnterface()
Context
+Contextlnterface()
ConcreteStrategyC
Context上下文,用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用
/// <summary> /// 打折收费子类 /// </summary> public class CashRebate : CashSuper-->ConcreteStrategyB { //打折 折扣 private double moneyRebate = 1d; public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } }
策略类,定义所有支持的算法的公共接口
具体策略类,封装了具体的算法活行为,继承于Strategy
Strategy
/// <summary> /// 正常收费子类 /// </summary> public class CashNormal : CashSuper-->ConcreteStrategyA { public override double acceptCash(double money) { return money; } }
//策略模式CashContext context = new CashContext(\"满300返10\");double result = context.GetResult(800);Console.WriteLine(result);
ConcreteStrategyB
0 条评论
下一页