策略模式(Strategy):它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。
实现目标:商场收银系统,实现正常收费、满300返100、打8折.......等不同收费方式
效果图:
HTML代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>设计模式:策略模式</title> <style type="text/css"> table { width: 300px; height: 250px; border-collapse: collapse; } table, tr, td { border: 1px solid gray; } </style> </head> <body> <form id="form1" runat="server"> <table> <thead> <tr> <td colspan="3"> 商场收银软件 </td> </tr> </thead> <tr> <td> 单价: </td> <td> <asp:TextBox runat="server" ID="txtPrice"></asp:TextBox> </td> <td> <asp:Button runat="server" ID="btnOK" Text="确定" onclick="btnOK_Click" /> </td> </tr> <tr> <td> 数量: </td> <td> <asp:TextBox runat="server" ID="txtNumber"></asp:TextBox> </td> <td> <asp:Button runat="server" ID="Button1" Text="重置" onclick="Button1_Click" /> </td> </tr> <tr> <td> 计算方式: </td> <td> <asp:DropDownList runat="server" ID="ddlCashType"> <asp:ListItem Selected="True">正常收费</asp:ListItem> <asp:ListItem>打8折</asp:ListItem> <asp:ListItem>满300返100</asp:ListItem> </asp:DropDownList> </td> <td> </td> </tr> <tr> <td colspan="3"> <asp:TextBox runat="server" ID=txtMsg TextMode="MultiLine" Rows="8" Columns="30"></asp:TextBox> </td> </tr> <tr> <td> 总计: </td> <td> <asp:Label runat="server" ID="lblTotal"></asp:Label> </td> <td> </td> </tr> </table> </form> </body> </html>
结构图:
CashContext类:
public class CashContext { CashSuper cs = null; public CashContext(string type) { switch (type) { case "正常收费": cs = new CashNormal(); break; case "满300返100": cs = new CashReturn("300", "100"); break; case "打8折": cs = new CashRebate("0.8"); break; } } public double GetResult(double money) { return cs.AcceptCash(money); } }
CashSuper类:
/// <summary> /// 现金收费抽象类 /// </summary> public abstract class CashSuper { public abstract double AcceptCash(double money); }
CashNormal类:
/// <summary> /// 正常收费子类 /// </summary> public class CashNormal : CashSuper { public override double AcceptCash(double money) { return money; } }
CashRebate类:
/// <summary> /// 打折收费子类 /// </summary> public 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; } }
CashReturn类:
/// <summary> /// 返利收费子类 /// </summary> public 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; } }
客户端实现:
protected void btnOK_Click(object sender, EventArgs e) { CashContext csuper = new CashContext(ddlCashType.SelectedValue); string totalPrice = csuper.GetResult(Convert.ToDouble(txtNumber.Text)*Convert.ToDouble(txtPrice.Text)).ToString(); lblTotal.Text = totalPrice; }
遗留问题:策略模式与工厂模式区别?????
原文:http://www.cnblogs.com/qianxingdewoniu/p/3540781.html