首页 > 其他 > 详细

设计模式:策略模式

时间:2014-02-09 15:50:22      阅读:344      评论:0      收藏:0      [点我收藏+]

策略模式(Strategy):它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。

 

实现目标:商场收银系统,实现正常收费、满300返100、打8折.......等不同收费方式

效果图:

bubuko.com,布布扣

HTML代码

bubuko.com,布布扣
<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>
View Code

结构图

bubuko.com,布布扣

CashContext类

bubuko.com,布布扣
  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);
        }
    }
View Code

CashSuper类: 

bubuko.com,布布扣
/// <summary>
    /// 现金收费抽象类
    /// </summary>
    public abstract class CashSuper
    {
        public abstract double AcceptCash(double money);
    }
View Code

CashNormal类:  

bubuko.com,布布扣
/// <summary>
    /// 正常收费子类
    /// </summary>
    public class CashNormal : CashSuper
    {
        public override double AcceptCash(double money)
        {
            return money;
        }
    }
View Code

CashRebate类

bubuko.com,布布扣
 /// <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;
        }
    }
View Code

CashReturn类

bubuko.com,布布扣
 /// <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;
        }
    }
View Code

客户端实现

bubuko.com,布布扣
        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;
        }
View Code

 

遗留问题:策略模式与工厂模式区别?????

设计模式:策略模式

原文:http://www.cnblogs.com/qianxingdewoniu/p/3540781.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!