一:策略模式概念
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合(DPE)当不同的行为堆砌在一个类中时,就很难避免使用条件语句来选择合适的行为。将这些行为封装在一个个独立的Strategy类中,可以再使用这些行为的类中消除条件语句(DP)
二:具体代码演示
1: 策略类
public abstract class CashSuper//策略类,定义所有支持算法的公共接口 { public abstract double acceptCash(double money); }
2:上下文类
public class CashContext { private CashSuper cs;//声明一个CashContext,上下文类 public CashContext(CashSuper csuper)//通过构造方法,传入具体的策略对象 { this.cs = csuper; } public double GetResult(double money)//根据具体的策略对象,调用其算法的方法 { return cs.acceptCash(money); } //-------------------------调用实例---------- // CashContext con=new CashContext(new CashNormal()); // con.GetResult(money) }
3:正常收费类
public class CashNormal:CashSuper//正常收费子类 { public override double acceptCash(double money) { return money; } }
4:打折类
public class CashRebate:CashSuper//打折类 { private double moneyRebate = 1d; public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate);//打折收费率,初始化时,必须要输入折扣率,如八折,就是0.8 } public override double acceptCash(double money) { return money*moneyRebate;//钱乘折扣率 } }
5:返利类
public class CashReturn:CashSuper//返利类 { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CashReturn(string moneyCondition, string moneyReturn)//返收利费,初始化时必须要输入返利条件和返利值,比如满300返100,则moneyCondition为300,moneyReturn为100 { this.moneyCondition = double.Parse(moneyCondition); this.moneyReturn = double.Parse(moneyReturn); } public override double acceptCash(double money) { double result = money; if (money >= moneyCondition)//若大于返利值,则需要减去返利值 result = money - Math.Floor(money/moneyCondition)*moneyReturn; return result; } }
6:客户端界面
7:客户端后台类
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private double total = 0.0d;//用于总计 private void button1_Click(object sender, EventArgs e)//确定按钮事件 { CashContext cc = null; switch (comboBox1.SelectedItem.ToString()) { case"正常收费": cc=new CashContext(new CashNormal()); break; case "满300返100": cc = new CashContext(new CashReturn("300","100")); break; case "打8折": cc = new CashContext(new CashRebate("0.8")); break; } double totalPrices = 0d; totalPrices = cc.GetResult(Convert.ToDouble(textBox1.Text)*Convert.ToDouble(textBox2.Text)); total = total + totalPrices; listBox1.Items.Add("单价:" + textBox1.Text + "数量:" + textBox2.Text+" "+comboBox1.SelectedItem+" 合计:"+totalPrices.ToString()); label5.Text = total.ToString(); } private void button2_Click(object sender, EventArgs e)//重置按钮事件 { textBox1.Text = ""; textBox2.Text = ""; listBox1.Text = ""; } }
三:策略与工厂相结合CashContext的代码变化如下
public class CashContext { private CashSuper cs;//声明一个CashContext,上下文类 //public CashContext(CashSuper csuper)//通过构造方法,传入具体的策略对象 //{ // this.cs = csuper; //} 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 double GetResult(double money)//根据具体的策略对象,调用其算法的方法 { return cs.acceptCash(money); } //-------------------------调用实例---------- // CashContext con=new CashContext(new CashNormal()); // con.GetResult(money) }
四:策略和工厂相结合的客户端代码变化如下
private void button1_Click(object sender, EventArgs e)//确定按钮事件 { CashContext cc=new CashContext(comboBox1.SelectedItem.ToString());//使用策略和工厂相结合需要的 double totalPrices = 0d; totalPrices = cc.GetResult(Convert.ToDouble(textBox1.Text)*Convert.ToDouble(textBox2.Text)); total = total + totalPrices; listBox1.Items.Add("单价:" + textBox1.Text + "数量:" + textBox2.Text+" "+comboBox1.SelectedItem+" 合计:"+totalPrices.ToString()); label5.Text = total.ToString(); }