策略模式学习代码(用反射实现超市收银功能)

来源:互联网 发布:求导软件手机版 编辑:程序博客网 时间:2024/04/28 00:48

 //后台代码

namespace Calculator
{
    //商场管理策略模式(反射)
    public partial class Shop : Form
    {
        public Shop()
        {
            InitializeComponent();
        }

        DataSet ds;             //用于存放配置文件信息
        double total = 0.0d;    //用于总计

        private void Shop_Load(object sender, EventArgs e)
        {
            //读取配置文件
            ds = new DataSet();
            ds.ReadXml(Application.StartupPath+"\\CashAcceptType.xml");
            //将读取到的记录绑定到下拉列表框中
            foreach (DataRowView dr in ds.Tables[0].DefaultView)
            {
                cbxType.Items.Add(dr["name"].ToString());
            }
            cbxType.SelectedIndex = 0;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            CashContext cc = new CashContext();
            //根据用户选项,查询用户选择项的相关行
            DataRow dr = ((DataRow[])ds.Tables[0].Select("name='" + cbxType.SelectedItem.ToString() + "'"))[0];
            //声明一个对象参数数组
            object[] args = null;
            //若有参数则将参数分割成字符串数组,用于实例化时所用的参数
            if (dr["para"].ToString() != "")
                args = dr["para"].ToString().Split(',');
           
            //通过反射实例化出相应的算法对象
            cc.setBehavior((CashSuper)Assembly.Load("Calculator").CreateInstance("Calculator." + dr["class"].ToString(), false, BindingFlags.Default, null, args, null, null));
            //cc.setBehavior(new CashNormal());

            double totalPrices = 0d;
            totalPrices = cc.GetResult(Convert.ToDouble(tb_Price.Text)*Convert.ToDouble(tb_Count.Text));
            total = total + totalPrices;
            lb_List.Items.Add("单价:"+tb_Price.Text+" 数量:"+tb_Count.Text+" "+cbxType.SelectedItem+" 合计"+totalPrices.ToString());
            lb_total.Text = total.ToString();
        }

    }
}


 

//计算模式类

namespace Calculator
{
    class CashContext
    {
        //声明一个现金收费父类对象
        private CashSuper cs;

        public void setBehavior(CashSuper csuper)
        {
            this.cs = csuper;
        }

        public double GetResult(double money)
        {
            return cs.acceptCash(money);
        }
    }

    //现金收取父类
    abstract class CashSuper
    {
        //抽象方法:收取现金,参数为原价,返回为当前价
        public abstract double acceptCash(double money);
    }

    //正常收费,继承CashSuper
    class CashNormal : CashSuper
    {
        public override double acceptCash(double money)
        {
            return money;
        }
    }

    //打折收费,继承CashSuper
    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;
        }
    }

    //返利收费,CashSuper
    class CashReturn : CashSuper
    {
        private double moneyCondition = 0.0d;
        private double moneyReturn = 0.0d;
        //初始化时必须要输入返利条件和返利值,比如慢300返100
        //则moneyCondition为300,moneyReturn为100
        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;
        }
    }

}

 

 

//XML数据

<?xml version="1.0" encoding="utf-8" ?>
<CashAcceptType>
  <type>
    <name>正常收费</name>
    <class>CashNormal</class>
    <para></para>
  </type>
  <type>
    <name>满300返100</name>
    <class>CashReturn</class>
    <para>300,100</para>
  </type>
  <type>
    <name>满200返50</name>
    <class>CashReturn</class>
    <para>200,50</para>
  </type>
  <type>
    <name>打8折</name>
    <class>CashRebate</class>
    <para>0.8</para>
  </type>
  <type>
    <name>打7折</name>
    <class>CashRebate</class>
    <para>0.7</para>
  </type>
</CashAcceptType>