标签:
策略模式定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。
using System; using System.Collections.Generic; using System.Text; namespace 策略模式 { class Program { static void Main(string[] args) { Context context; context = new Context(new ConcreteStrategyA()); context.ContextInterface(); context = new Context(new ConcreteStrategyB()); context.ContextInterface(); context = new Context(new ConcreteStrategyC()); context.ContextInterface(); Console.Read(); } } //抽象算法类 abstract class Strategy { //算法方法 public abstract void AlgorithmInterface(); } //具体算法A class ConcreteStrategyA : Strategy { //算法A实现方法 public override void AlgorithmInterface() { Console.WriteLine("算法A实现"); } } //具体算法B class ConcreteStrategyB : Strategy { //算法B实现方法 public override void AlgorithmInterface() { Console.WriteLine("算法B实现"); } } //具体算法C class ConcreteStrategyC : Strategy { //算法C实现方法 public override void AlgorithmInterface() { Console.WriteLine("算法C实现"); } } //上下文 class Context { Strategy strategy; public Context(Strategy strategy) //传递任何符合要求的算法进来都可以,有利于扩展~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ { this.strategy = strategy; } //上下文接口 public void ContextInterface() { strategy.AlgorithmInterface(); } } }
四、策略模式应用
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; namespace 商场管理软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d;//用于总计 private void btnOk_Click(object sender, EventArgs e) { CashContext cc = null; switch (cbxType.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(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " " + cbxType.SelectedItem + " 合计:" + totalPrices.ToString()); lblResult.Text = total.ToString(); } private void btnClear_Click(object sender, EventArgs e) { total = 0d; txtPrice.Text = "0.00"; txtNum.Text = "1"; lbxList.Items.Clear(); lblResult.Text = "0.00"; } private void Form1_Load(object sender, EventArgs e) { } } }
//收费策略Context class CashContext { //声明一个现金收费父类对象 private CashSuper cs; //设置策略行为,参数为具体的现金收费子类(正常,打折或返利) public CashContext(CashSuper csuper) { this.cs = csuper; } //得到现金促销计算结果(利用了多态机制,不同的策略行为导致不同的结果) public double GetResult(double money) { return cs.acceptCash(money); } }
abstract class CashSuper { public abstract double acceptCash(double money); }
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 result = money; if (money >= moneyCondition) result=money- Math.Floor(money / moneyCondition) * moneyReturn; return result; } }
class CashRebate : CashSuper { private double moneyRebate = 1d; public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } }
class CashNormal : CashSuper { public override double acceptCash(double money) { return money; } }
五、策略模式和简单工厂的结合应用
namespace 商场管理软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //客户端窗体程序(主要部分) double total = 0.0d; private void btnOk_Click(object sender, EventArgs e) { //利用简单工厂模式根据下拉选择框,生成相应的对象 CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = 0d; //通过多态,可以得到收取费用的结果 totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " " + cbxType.SelectedItem + " 合计:" + totalPrices.ToString()); lblResult.Text = total.ToString(); } private void btnClear_Click(object sender, EventArgs e) { total = 0d; txtPrice.Text = "0.00"; txtNum.Text = "0"; lbxList.Items.Clear(); lblResult.Text = "0.00"; } } }
//收费策略Context和简单工厂结合 class CashContext { CashSuper cs = null; //根据条件返回相应的对象 public CashContext(string type) { switch (type) { case "正常收费": CashNormal cs0 = new CashNormal(); cs = cs0; break; case "满300返100": CashReturn cr1 = new CashReturn("300", "100"); cs = cr1; break; case "打8折": CashRebate cr2 = new CashRebate("0.8"); cs = cr2; break; } } public double GetResult(double money) { return cs.acceptCash(money); } }
标签:
原文地址:http://www.cnblogs.com/kesimin/p/4843725.html