/// <summary> /// 通过构造的方式,创建出用哪种策略对象 /// </summary> /// <param name="type"></param> public ContextStrategy(string type) { //工厂和策略的结合 switch (type) { case "正常收费": CS = new CashNormal(); break; case "打8折": CS = new CashRebate("0.8"); break; default: CS = new CashNormal(); break; } }
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; default: break; } }
/// <summary> /// /// </summary> /// <param name="type">收费策略,而不是收费的对象</param> public CashContext(string type) { switch (type) { case "1": CashNormal cs0 = new CashNormal(); //实例化具体策略的过程由客户端转移到Context类中。简单工厂的应用 cs = cs0; break; case "2": CashReturn cs1 = new CashReturn("300", "100"); cs = cs1; break; case "3": CashRebate cs2 = new CashRebate("0.8"); cs = cs2; break; } }
public CashContext(string type) { switch (type) { case "正常收費": var cs0 = new CashNormal(); cs = cs0; break; case "滿300送100": var cr1 = new CashReturn("300", "100"); cs = cr1; break; case "打8折": var cr2 = new CashRebate("0.8"); cs = cr2; break; } }
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 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); }
static void Main(string[] args) {//還是工廠 CashFactory CashSuper會看到2個類別 double total = 0d; CashSuper cs = CashFactory.createCashAccept("打8折"); double totalprice = 0d; totalprice = cs.acceptCash(5000d) * 5;//price*num total += totalprice; Console.WriteLine(total); //策略模式 只需要一個CashContext類別 收費演算法跟用戶端分離 CashContext cc = null; cc = new CashContext("打8折"); totalprice = 0d; totalprice = cc.GetResult(5000) * 5; //單價X數量 total += totalprice; Console.WriteLine(total); Console.ReadKey(); }
public static CashSuper createCashAccept(string type) { CashSuper cashSuper = null; switch (type) { case "正常收費": cashSuper = new CashNormal(); break; case "打8折": cashSuper = new CashRebate(0.8d); break; case "滿300送100": cashSuper = new CashReturn(300d, 100d); break; } return(cashSuper); }
public static CashSuper createCashAccept(string type) { CashSuper cs = null; switch (type) { case "正常收费": cs = new CashNormal(); break; case "满300返100": CashReturn cr1 = new CashReturn("300", "100"); cs = cr1; break; case "打8折": CashRebate cr2 = new CashRebate("0.8"); cs = cr2; break; } return(cs); }
public CashContext(CashSuper cashSuper) { this.cashSuper = cashSuper; }