大话设计模式 第二章:策略模式
ZZ: http://www.cnblogs.com/wang7/archive/2012/09/12/2681393.html
策略模式(strategy):它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到试用算法的客户。
本文章是的工厂模式和策略模式的结合例子:
1,定义现金抽象类:
/// <summary> /// 现金父类 /// </summary> public abstract class CashSuper { /// <summary> /// 将原价转为返回价 /// </summary> /// <param name="money">原价</param> public abstract double acceptCash(double money); }
2,定义各个算法类,全部继承自现金父类:
![大话设计模式 第二章:策略模式 - Lexlin - lexlin的博客](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
/// <summary> /// 没有活动的现金类 /// </summary> public class CashNormal : CashSuper { public override double acceptCash(double money) { return money;//直接返回原价 } } /// <summary> /// 打折类 /// </summary> public class CashRebate : CashSuper { private double rebate = 1.0;//打几折 public CashRebate(double rebate) { this.rebate = rebate; } public override double acceptCash(double money) { double result = 0.0; result = money * rebate; return result; } } /// <summary> /// 返回现金类 /// </summary> public class CashReturn : CashSuper { private double moneyCondition;//满多少钱 private double moneyReturn; //返多少钱 public CashReturn(double moneyCondition, double moneyReturn) { this.moneyCondition = moneyCondition; this.moneyReturn = moneyReturn; } public override double acceptCash(double money) { double result = 0.0; result = money - Math.Floor(money / moneyCondition) * moneyReturn; return result; } }
3,定义现金上下文类,决定使用哪一种算法,并实例化哪个算法对象,这是工厂模式与策略模式的结合:
![大话设计模式 第二章:策略模式 - Lexlin - lexlin的博客](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
/// <summary> /// 现金上下文,选择哪种活动方式 /// </summary> public class CashContext { private CashSuper cash=null; /// <summary> /// 根据活动方式返回价格 /// </summary> /// <param name="type">活动方式(如:打8折)</param> public CashContext(string type) { switch (type) { case "正常收费": cash = new CashNormal(); break; case "打8折": cash = new CashRebate(0.8); break; case "满300返100": cash = new CashReturn(300, 100); break; default: throw new Exception("没有该活动"); } } /// <summary> /// 输入原价返回价格 /// </summary> /// <param name="money">原价</param> public double GetResult(double money) { return cash.acceptCash(money); } }
4,客户端界面的设计:
5,客户端后台代码:
![大话设计模式 第二章:策略模式 - Lexlin - lexlin的博客](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0; private void buttonOK_Click(object sender, EventArgs e) { try { double totalPrice = 0.0; CashContext cashContext = new CashContext(this.comboBox1.Text); totalPrice = cashContext.GetResult(Convert.ToDouble(textBoxPrice.Text) * Convert.ToDouble(textBoxNum.Text)); string resutl = "单价:" + textBoxPrice.Text + " 数量:" + textBoxNum.Text + " 活动方式:" + comboBox1.SelectedText + " 总价:" + totalPrice.ToString(); listBox1.Items.Add(resutl); total += totalPrice; this.labelTotal.Text = total.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { string[] type = new string[] { "正常收费", "打8折", "满300返100" }; this.comboBox1.Items.AddRange(type); } }