Implement Strategy
Implement Strategy
Implement Strategy
Goal:
define CashContext
client use new CashContext(new class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class CashContext { private CashSuper cs; public CashContext(CashSuper csuper) { this.cs = csuper; } public double GetResult(double money) { return cs.acceptCash(money); } }
static void Main(string[] args) { CashContext cc = null; switch (args[0]) { case "Normal": cc = new CashContext(new CashNormal()); break; case "Return": cc = new CashContext(new CashReturn("300", "100")); break; case "Rebate": cc = new CashContext(new CashRebate("0.8")); break; } double totalPrice = cc.GetResult(100 * 100); }
|