商场促销 策略模式
2.1 商场收银软件
namespace 商场管理软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d; private void btnOk_Click(object sender, EventArgs e) { double totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); total = total + totalPrices; lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 总计:" + 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"; } } }
2.2 增加打折
namespace 商场管理软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d; private void Form1_Load(object sender, EventArgs e) { cbxType.Items.AddRange(new object[] { "正常收费", "打八折", "打七折", "打五折" }); cbxType.SelectedIndex = 0; } private void btnOk_Click(object sender, EventArgs e) { double totalPrices = 0d; switch (cbxType.SelectedIndex) { case 0: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); break; case 1: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.8; break; case 2: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.7; break; case 3: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.5; break; } 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"; } } }
2.3 简单工厂实现
面向对象编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类,
abstract class CashSuper { public abstract double acceptCash(double money); } //正常收费 class CashNormal : CashSuper { public override double acceptCash(double money) { return money; } } //返利收费 class CashReturn : CashSuper { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; //初始化时必须输入返利条件和返利值,如满300返100,则moneyCondition为300,moneyReturn为100, 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; //初始化时必需要输入折扣率,如八折就是0.8, public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } } //现金收取工厂 class CashFactory { //根据条件返回相应的对象 public static CashSuper createCashAccept(string type) { CashSuper cs = null; switch (type) { case "正常收费": cs = new CashNormal(); break; case "满300返100": CashReturn cr1 = new CashReturn("300", "100"); cs = cr1; break; case "打8折": CashRebate cr2 = new CashRebate("0.8"); cs = cr2; break; } return cs; } }
namespace 商场管理软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d; private void btnOk_Click(object sender, EventArgs e) { CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = csuper.acceptCash(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"; } } }
2.4 策略模式
策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化不会影响到使用算法的客户,
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(); } } }
2.5 策略模式实现
abstract class CashSuper { public abstract double acceptCash(double money); } //正常收费 class CashNormal : CashSuper { public override double acceptCash(double money) { return money; } } //返利收费 class CashReturn : CashSuper { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; //初始化时必须输入返利条件和返利值,如满300返100,则moneyCondition为300,moneyReturn为100, 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; //初始化时必需要输入折扣率,如八折就是0.8, public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } } class CashContext { private CashSuper cs; public CashContext(CashSuper csuper) { this.cs = csuper; } public double GetResult(double money) { return cs.acceptCash(money); } }
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"; } } }
2.6 策略与简单工厂结合
abstract class CashSuper { public abstract double acceptCash(double money); } //正常收费 class CashNormal : CashSuper { public override double acceptCash(double money) { return money; } } //返利收费 class CashReturn : CashSuper { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; //初始化时必须输入返利条件和返利值,如满300返100,则moneyCondition为300,moneyReturn为100, 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; //初始化时必需要输入折扣率,如八折就是0.8, public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } } 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); } }
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"; } } }
2.7 策略模式分析
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合,
策略模式的Strategy类层次为Context定义了一系列的可供重用的算法或行为,继承有助于析取出这些算法中的公共功能,
策略模式的优点是简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试,
策略模式就是用来封装算法的,在实践中可以用它来封装几乎任何类型的规则,只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性,