一个策略模式的实现

今天执着看《大话模式设计》,又学到一个很重要的东东:第二种模式:策略模式:

我们来看一个关于收银系统:它的界面是这样的:

和以前一样我们选来看看业务逻辑层的代码:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace winform1
{

    public abstract class CashSuper//抽象类(因为它存在抽象方法)
    {
        public abstract double acceptCash(double money);//写一个抽象方法,继承此类的类必须重写此方法
    }

    //正常收费子类,也就是说即不打折,也不返现
    public class CashNormal : CashSuper
    {
        public  override  double acceptCash(double money)
        {
            return money;
        }
    }

    //打折收费子类
    public class CashRebate:CashSuper 
    {
        private double  theRebate;
        public CashRebate(double Rebate)
        {
            theRebate = Rebate;
        }

        public override  double acceptCash(double money)
        {
            return money * Convert.ToDouble(theRebate);
        }
    }
    //返现子类
    public class CasheReturn : CashSuper
    {
        private double fullMoney;
        private double returnMoney;
        public CasheReturn(double fullM, double returM)
        {
            fullMoney = fullM;
            returnMoney = returM;
        }

        public  override  double  acceptCash(double money)
        {
            if (money >= fullMoney)//返现条件
            {
                return money - Math.Floor(money / fullMoney) * returnMoney;
            }
            else
            {
                return money;
            }
        }
    }

    //设计一个策略模式,注意此模式在与简单工厂模式混合使用
    public class CashContxt
    {
        CashSuper CS;
        double reMoney;
        public CashContxt(string type)
        {
            switch (type)
            {
                case "正常收费":
                    {
                        this.CS = new CashNormal();
                    } break;
                case "满300返100":
                    {
                        this.CS = new CasheReturn(300, 100);
                    } break;
                case "打八折":
                    {
                        this.CS = new CashRebate(0.8);
                    } break;
            }
        }

        public double  CashContextInterface(double money)
        {
            reMoney = CS.acceptCash(money);//这个外观上就是调用抽象类中的抽象方法
            return reMoney;
        }
    }



    //设计一个简单工厂模式,用此模式一样的可以完成系统功能(但本例主要使用上面的模式完成)
    public class CashFactory
    {
        public static CashSuper CreateCashAccept(string type)
        {
            CashSuper cs = null;
            switch (type)
            {
                case "正常收费":
                    {
                        cs = new CashNormal();
                    } break;
                case "满300返100":
                    {
                        cs = new CasheReturn(300, 100);
                    } break;
                case "打八折":
                    {
                        cs = new CashRebate(0.8);
                    } break;
            }

            return cs;
        }
    }

}
 
好了,我们再来看看客户端的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winform1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        double totalPrice = 0.00;

        //计算总价,客户端代码
        private void button1_Click(object sender, EventArgs e)
        {
            double nowPrice = Convert.ToDouble(this.txtPrice.Text ) * Convert.ToDouble(this.txtCount.Text);
            string type = this.comboBox1.SelectedItem.ToString();
            CashContxt cctxt;
            cctxt = new CashContxt(type);
            double truePrice = cctxt.CashContextInterface(nowPrice);
            totalPrice = totalPrice + truePrice;
            this.listBox1.Items.Add("单价:" + this.txtPrice.Text + "  " + "数量:" + this.txtCount.Text + "类型:" + type + "总价:" + truePrice.ToString());
            this.label4.Text = totalPrice.ToString();
        }
        //清空
        private void button2_Click(object sender, EventArgs e)
        {
            this.txtCount.Text = "";
            this.txtPrice.Text = "";
        }
    }
}
其实我对此模式也不是很了解,只是今晚看了一下,觉得还可以理解,所以就把它贴出来与大家共同讨论,共同学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值