代码改变世界

大话设计模式之策略模式

2014-04-21 13:47  微尘_无名  阅读(198)  评论(0编辑  收藏  举报

Strategy(策略)模式:定义了一个算法家族,分别封装起来,这些算法之间可以互相切换,此模式让算法的变化,不会影响到使用算法的客户。简而言之,策略模式封装了变化,此变化即为算法。使用场合为:在不同的时候要使用不同的业务规则,可以考虑使用策略模式处理这种变化的可能性。

本文以超市收银软件为说明蓝图:

业务逻辑规则:

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

客户端:

double total = 0.0d;
        private void Form1_Load(object sender, EventArgs e)
        {
            cbxType.Items.AddRange(new object[] { "正常收费","满300返100", "打8折"});
            cbxType.SelectedIndex = 0;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string type = cbxType.SelectedItem.ToString();
            CashContext cc = new CashContext(type);
            double totalPrices = cc.getResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
            total += totalPrices;
            lbxList.Items.Add("单价: " + txtPrice.Text + " 数量: " + txtNum.Text + " " + cbxType.SelectedItem + " 合计:" + totalPrices.ToString());
            lblResult.Text = total.ToString();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtPrice.Text = "";
            txtNum.Text = "";
            cbxType.SelectedIndex = 0;
            lblResult.Text = "";
            lbxList.Items.Clear();
        }
View Code