// 与简单工厂模式相结合,根据type,context决定用那种策略。主程序就只需要知道CashContext类,而具体策略类也可以不知道。 // 至于这里具体的实现,根据不同的情况,需要的参数不同可以根据重载来实现 public CashContext(string type) { switch (type) { case "正常": _cal = new CashNormal();break; case "打折": _cal = new CashRate(0.8);break; case "返现": _cal = new CashReturn(300, 100);break; default: throw new Exception("选择打折错误!"); } }
public CashFactoryAndContent(string cashType) { switch (cashType) { case "正常": cs = new CashNormal(); break; case "满三百返一百": cs = new CashReturn(300, 100); break; case "打0.8折": cs = new CashDisCount(0.8); break; default: break; } }
public CashContext(string type) { switch (type) { case "正常收费": cashSuper = new CashNormal(); break; case "满300返100": cashSuper = new CashReturn("300", "100"); break; case "打8折": cashSuper = new CashRebate("0.8"); break; } }
public CashContext(string type) { switch (type) { case "normal": cash = new CashNormal(); break; case "100per300": cash = new CashReturn(300, 100); break; case "0.8": cash = new CashRebate(0.8); break; default: break; } }
public CashContext(string type) { switch (type) { case "正常收费": CashNormal cs0 = new CashNormal(); cs = cs0; break; case "满300返100": CashReturn cs1 = new CashReturn("300", "100"); cs = cs1; break; case "打8折": CashRebate cs2 = new CashRebate("0.8"); cs = cs2; 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; default: break; } return(cs); }
public CashContext(CashSuper cal) { _cal = cal; }
public CashContext(CashSuper csuper) { this.cs = csuper; }
/// <summary> /// 初始化传入策略对象 /// </summary> /// <param name="cs"></param> public CashContent(CashSuper cs) { this.cs = cs; }
/// <summary> /// 工厂模式运行 /// </summary> static void RunFactory() { CashSuper cash = CashFactory.CreateCashAccept("打折收费"); Console.WriteLine(cash.AcceptCash(800)); }