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

    UML:


    CashSuper(父类)

     /// <summary>
        /// 2014-05-21
        /// 超市商品收费
        /// </summary>
        public abstract class CashSuper
        {
            public abstract double AcceptCash(double money);
        }
    View Code

    子类:

     /// <summary>
        /// 普通收费
        /// </summary>
        public class NormalCashSuper : CashSuper
        {
            public override double AcceptCash(double money)
            {
                return money;
            }
        }
    View Code
     /// <summary>
        /// 打折收费
        /// </summary>
        public class RebateCashSuper : CashSuper
        {
            public double MoneyRebate
            {
                get;
                set;
            }
            public RebateCashSuper(double moneyRebate)
            {
                this.MoneyRebate = moneyRebate;
            }
            public override double AcceptCash(double money)
            {
                return this.MoneyRebate * money ;
            }
        }
    View Code
     /// <summary>
        /// 返利收费
        /// </summary>
        public class ReturnCashSuper : CashSuper
        {
            /// <summary>
            /// 返利金额条件
            /// </summary>
            public double MoneyCondition
            {
                get;
                set;
            }
            /// <summary>
            /// 返利金额
            /// </summary>
            public double ReturnMoney
            {
                get;
                set;
            }
            public override double AcceptCash(double money)
            {
                if (money >= this.MoneyCondition)
                {
                    return money  - this.ReturnMoney;
                }
                else
                {
                    return money;
                }
            }
        }
    View Code

    策略类:

     public class CashContext
        {
            private CashSuper cashSuper = null;
            public CashContext(CashSuper cs)
            {
                this.cashSuper = cs;
            }
          
    
            public double GetResult(double money)
            {
               double returnMoney = this.cashSuper.AcceptCash(money);
               return returnMoney;
            }  
            
        
        }
    View Code

    页面代码:

    protected void btnStrategy_Click(object sender, EventArgs e)
            {
                CashContext cc = null;
                int intType = int.Parse(this.DropDownList1.SelectedValue.Trim());
                double dbRebate = double.Parse(this.txtRebate.Text.Trim());
                double dbConditonMoney = double.Parse(this.txtCondition.Text.Trim());
                double dbReturnMoney = double.Parse(this.txtReturnMoney.Text.Trim());
                double dbMoney = double.Parse(this.txtMoney.Text.Trim());  
                switch (intType)
                { 
                    case 1:
                        cc = new CashContext(new NormalCashSuper());
                        break;
                    case 2:
                        RebateCashSuper rcs = new RebateCashSuper(dbRebate);
                        cc = new CashContext(rcs);
                        break;
                    case 3:
                        ReturnCashSuper recs = new ReturnCashSuper();
                        recs.MoneyCondition = dbConditonMoney;
                        recs.ReturnMoney = dbReturnMoney;
                        cc = new CashContext(recs);
                        break;
                }
                Response.Write(cc.GetResult(dbMoney).ToString());  
     
            }
    View Code

    问题

    1:书中有提到将页面上的条件判断代码放到策略类中,那折扣,返利等数据如何传递?

      如下:public CashContext(int intType,.....?)
            {
                switch(inttype)

          {

            case 1:

              new NormalCashSuper();

              break;

          }
            }

    2:代码中的money指的是计算后的总价。如业务变化,需增加买5退一的业务(即买5个只算4个的价格),如何更改代码?

       此业务和以上三种业务的关系是什么(兄弟OR没关系)?

    总结:

    1:页面只访问了CashContext类,算法和客户端分离了。

    2:算法可以单独进行单元测试,互相不受影响。

    3:客户端代码简单了。

  • 相关阅读:
    webkit v8 chromium blink chrome 的关系
    webkit 系列
    工具使用过程中遇到问题
    ElasticSearch实战笔记
    办理北京市居住证需要哪些资料
    办理北京市居住证需要哪些资料
    MongoDB 笔记
    Javascript问题集锦
    sqlserver2016 management tool v18
    PostMan测试Web Service
  • 原文地址:https://www.cnblogs.com/sportdog/p/3745852.html
Copyright © 2011-2022 走看看