zoukankan      html  css  js  c++  java
  • 大话设计模式之策略模式

    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
  • 相关阅读:
    第十六章 课程总复习
    第四章 数据类型及数据库基本操作
    第二章.图形化管理工具
    第十三章 指导学习:人机猜拳
    洛谷 P4396 (离散化+莫队+树状数组)
    洛谷 P1351 (枚举)
    洛谷P5322 (BJOI 2019) DP
    P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)
    洛谷 P2176(最短路)
    HDU 6556 (2018CCPC吉林 B题)
  • 原文地址:https://www.cnblogs.com/wwblog/p/3678248.html
Copyright © 2011-2022 走看看