码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之策略模式

时间:2014-05-26 21:25:47      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   tar   

UML:

bubuko.com,布布扣


CashSuper(父类)

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

子类:

bubuko.com,布布扣
 /// <summary>
    /// 普通收费
    /// </summary>
    public class NormalCashSuper : CashSuper
    {
        public override double AcceptCash(double money)
        {
            return money;
        }
    }
View Code
bubuko.com,布布扣
 /// <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
bubuko.com,布布扣
 /// <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

策略类:

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

页面代码:

bubuko.com,布布扣
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:客户端代码简单了。

 

 

 

 

设计模式之策略模式,布布扣,bubuko.com

设计模式之策略模式

标签:style   c   class   blog   code   tar   

原文地址:http://www.cnblogs.com/sportdog/p/3745852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!