策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合
策略模式封装了变化
在实践中,我们发现可以用它来封装几乎任类型的规则,只要在分析过程中听到需要在不同时间应用到不同的业务规则,就考虑使用策略模式来处理这种变化的可能
商场促销例子
现金收费抽象类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace factoryClass.Strategy { /// <summary> /// 现金收费抽象类 /// </summary> public abstract class CashSuper { /// <summary> /// 超类的抽象方法,收取现金 /// </summary> /// <param name="money">参数为原价</param> /// <returns>返回为当前价</returns> public abstract double acceptCash(double money); } }
正常收费子类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace factoryClass.Strategy { /// <summary> /// 正常收费子类 /// </summary> public class CashNormal:CashSuper { public override double acceptCash(double money) { return money; } } }
打折收费子类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace factoryClass.Strategy { /// <summary> /// 打折收费子类 /// </summary> public class CashRebate:CashSuper { private double moneyRebate = 1d; public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return moneyRebate * money; } } }
返利收费子类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace factoryClass.Strategy { /// <summary> /// 返利收费子类 /// </summary> public 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 resurt = money; if (money >= moneyCondition) { resurt = money - Math.Floor(money / moneyCondition) * moneyReturn; } return resurt; } } }
CashContext类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace factoryClass.Strategy { public class CashContext { CashSuper cs=null;//申明一个CashSuper对象 /// <summary> /// CashContext /// </summary> /// <param name="type">促销类型</param> /// <param name="moneyRebates">折扣</param> /// <param name="moneyCondition">满</param> /// <param name="moneyReturn">返</param> public CashContext(string type, string moneyRebates, string moneyCondition, string moneyReturn) { switch (type) { case "0": CashNormal cs0 = new CashNormal(); cs = cs0; break; case "1": CashRebate cs1 = new CashRebate(moneyRebates); cs = cs1; break; case "2": CashReturn cs2 = new CashReturn(moneyCondition, moneyReturn); cs = cs2; break; } } /// <summary> /// 根据收费策略不同,获得计算结果 /// </summary> /// <param name="money"></param> /// <returns></returns> public double GetResult(double money) { return cs.acceptCash(money); } } }
客户端
protected void btnA_Click(object sender, EventArgs e) { factoryClass.Strategy.CashContext cashContext = new factoryClass.Strategy.CashContext(this.ddlcu.SelectedValue,this.ddlzhe.SelectedValue,this.txtman.Text,this.txtfan.Text); this.txtying.Text = cashContext.GetResult(Convert.ToDouble(this.txtprice.Text)).ToString(); }
时间: 2024-10-03 21:10:32