大话设计模式读书笔记(三)策略模式

策略模式实现商场收费:

策略模式基本代码

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(); } } }

 

大话设计模式读书笔记(三)策略模式_第1张图片

 

参照前一篇文章,简单工厂实现商场收费,CashSuper、CasheNormal、CashRebate和CasheReturn都不用修改只要加上CashContext类,并修改一下客户端就行了。

 

CashContext类:

 using System; using System.Collections.Generic; using System.Text; namespace 商场管理软件 { //收费策略Context class CashContext { //声明一个现金收费父类对象 private CashSuper cs;//声明一个CashSuper对象 //设置策略行为,参数为具体的现金收费子类(正常,打折或返利) public CashContext(CashSuper csuper) { this.cs = csuper; } //得到现金促销计算结果(利用了多态机制,不同的策略行为导致不同的结果) public double GetResult(double money) { return cs.acceptCash(money); } } }

 

 

策略模式前台代码: 

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"; } } }

 

 

 

 策略模式与简单工厂结合实现商场收费

 

改造后的CashContext

 using System.Text; namespace 商场管理软件 { //现金收取工厂 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); } } }

 

客户端代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; 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"; } } }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(设计模式,算法,object,String,读书,Class)