zoukankan      html  css  js  c++  java
  • 策略模式与工厂结合(实现商场收银效果)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace 商场收银
    {
        abstract class CashSuper
        {
            /// <summary>
            /// 现金收费抽象类
            /// </summary>
            /// <param name="money">原价</param>
            /// <returns>折扣价格</returns>
            public abstract double acceptCash(double money);
        }
     
        /// <summary>
        /// 二合一的具体效果实现1/2
        /// </summary>
        class CashContext
        {
            private CashSuper cs;
     
            /// <summary>
            ///构造函数,让传入具体的不同的子类实例化对象     
            /// </summary>
            /// <param name="type"></param>
            public CashContext(string type)
            {
                switch (type)
                {
                    case "正常收费":
                        cs = new CashNormal();
                        break;
     
                    case "满300返100":
                        cs = new CashReturn("300", "100");
                        break;
     
                    case "打八折":
                        cs = new CashRebate("0.8");
                        break;
                }
            }
     
            public double GetResult(double money)
            {
                return cs.acceptCash(money);
            }
     
        }
     
        /// <summary>
        /// 简单工厂模式的具体实现效果1/2
        /// </summary>
        class CashFactory
        {
            public static CashSuper createCashAccept(string type)
            {
                CashSuper cs = null;
                switch (type)
                {
                    case "正常收费":
                        cs = new CashNormal();
                        break;
     
                    case "满300返100":
                        cs = new CashReturn("300", "100");
                        break;
     
                    case "打8折":
                        cs = new CashRebate("0.8");
                        break;
                }
     
                return cs;
            }
        }
    }
     
     
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace 商场收银
    {
        class CashNormal : CashSuper
        {
            /// <summary>
            /// 正常收费,原价返回。
            /// </summary>
            /// <param name="money"></param>
            /// <returns></returns>
            public override double acceptCash(double money)
            {
                return money;
            }
        }
     
        /// <summary>
        /// 打折收费类
        /// </summary>
        class CashRebate : CashSuper
        {
            //折扣率
            private double moneyRebate = 1d;
            public CashRebate(string moneyRebate)
            {
                this.moneyRebate = double.Parse(moneyRebate);
            }
     
            /// <summary>
            /// 打折收费
            /// </summary>
            /// <param name="money"></param>
            /// <returns></returns>
            public override double acceptCash(double money)
            {
                return money * moneyRebate;
            }
     
        }
     
        /// <summary>
        /// 返利收费
        /// </summary>
        class CashReturn : CashSuper
        {
            //例如,满300(moneyCondition),返回100(moneyReturn)。
            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;
            }
        }
    }
     
     
     
    //main函数实现
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace 商场收银
    {
        public partial class FormMain : Form
        {
            public FormMain()
            {
                InitializeComponent();
            }
     
            //double类型来统计总数。
            double total = 0.0d;
     
            private void btnOk_Click(object sender, EventArgs e)
            {
                double totalPrices = 0d;
     
                ////简单工程的实现方式2/2
                //CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
                //totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
     
                ////策略模式的实现方式2/2
                //CashContext cc = null;
                //switch (cbxType.SelectedItem.ToString())
                //{
                //    case "正常收费":
                //        cc = new CashContext(new CashNormal());
                //        break;
     
                //    case "满300返100":
                //        cc = new CashContext(new CashReturn("300", "100"));
                //        break;
     
                //    case "打八折":
                //        cc = new CashContext(new CashRebate("0.8"));
                //        break;
                //}
                //totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
     
                //工厂策略二合一效果
                CashContext cc = new CashContext(cbxType.SelectedItem.ToString());
                totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
     
                total += totalPrices;
                lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 总计:" + totalPrices);
                lbMess.Text = total.ToString();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                total = 0;
                txtPrice.Text = "";
                txtNum.Text = "";
                lbMess.Text = null;
                lbxList.Items.Clear();
                cbxType.SelectedIndex = 0;
            }
     
            private void FormMain_Load(object sender, EventArgs e)
            {
                cbxType.Items.AddRange(new object[] { "正常收费", "满300返100", "打八折" });
                cbxType.SelectedIndex = 0;
            }
        }
    }
  • 相关阅读:
    struts2文件上传(多文件)文件下载
    Struts2拦截器
    MySQL中修改多个数据表的字段拼接问题
    Struts2接受请求参数三种常用方法
    struts2 配置详解
    Struts2入门问题
    Struts2启动问题:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    Ajax和json一道基本的练习题
    jQuery事件--blur()和focus()
    jQuery事件--mouseover()、mouseout()、mouseenter()和mouseleave()
  • 原文地址:https://www.cnblogs.com/loveyatou/p/2961630.html
Copyright © 2011-2022 走看看