二、策略模式之商场促销计价器

1、创建一个窗体项目,窗体内控件如图所示:

二、策略模式之商场促销计价器

 

2、相应的类如下

using System;

namespace 商场促销_策略模式实现
{
    // 现金收取超类的抽象方法,收取现金,参数为原价,返回为当前价
    abstract class CashSuper
    {
        public abstract double acceptCash(double money);
    }
    //正常收费,原价返回
    class CashNormal : CashSuper
    {
        public override double acceptCash(double money)
        {
            return money;
        }
    }
    //打折收费类,初始化时必须输入折扣率,如八折 0.8
    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;
        }
    }
    //返利收费类,初始化时必须要输入返利条件和返利值,如满300返100
    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 CashContext
    {
        CashSuper cs;   //声明一个CashSuper
        //通过构造方法,传入具体的收费策略
        public CashContext(string type) //参数type为字符串,表示收费策略
        {
            switch (type)   //将实例化具体策略的过程由客户端转移到Context类中,即简单工厂的应用
            {
                case "正常收费":
                    CashNormal cs0 = new CashNormal();
                    cs = cs0;
                    break;
                case "满300返100":
                    CashReturn cs1 = new CashReturn("300","100");
                    cs = cs1;
                    break;
                case "打八折":
                    CashRebate cs2 = new CashRebate("0.8");
                    cs = cs2;
                    break;
            }
        }
        //根据收费策略不同,获得计算结果
        public double GetResult(double money)
        {
            return cs.acceptCash(money);
        }

    }
}

 

3、窗体控件相关操作代码

 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace 商场促销_策略模式实现
 5 {
 6     public partial class Form1 : Form
 7     {
 8         public Form1()
 9         {
10             InitializeComponent();
11         }
12 
13         double total = 0.0d;    //用于总计
14 
15         private void btnOk_Click(object sender, EventArgs e)
16         {
17             string strCashAccept = cbxType.Text;    //声明计价类型
18             CashContext cc = new CashContext(cbxType.Text); //将选定的计价类型用于初始化对应的计价策略
19             double totalPrice = 0d;
20             totalPrice = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtTotal.Text));   //根据计价策略计算得到结果
21             total = total + totalPrice;
22             lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtTotal.Text + " " +    //显示计算得到的结果
23                 cbxType.SelectedItem + "\t合计:" + totalPrice.ToString());
24             lblResult.Text = total.ToString();
25         }
26 
27         private void btnClear_Click(object sender, EventArgs e)
28         {
29             txtPrice.Text = null;   //清空显示结果
30             txtTotal.Text = null;
31             lbxList.Items.Clear();
32             lblResult.Text = null;
33             total = 0d;
34         }
35     }
36 }

 

注:本文内容是《大话设计模式》一书阅读过程复写代码!

上一篇:java中FileInputStream和FileOutputStream对图片操作的例子


下一篇:设计模式与代码的结构特性