读《大话设计模式》——应用工厂模式的"商场收银系统"(WinForm)

  要做的是一个商场收银软件,营业员根据客户购买商品单价和数量,向客户收费。两个文本框,输入单价和数量,再用个列表框来记录商品的合计,最终用一个按钮来算出总额就可以了,还需要一个重置按钮来重新开始。

 

 

 

 

   

  核心代码(v1.0)

 //声明一个double变量total来计算总计
        double total = 0.0d;
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //声明一个double变量totalPrices来计算每个商品的单价(txtPrice) * 数量(txtNum)后的合计
            double totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text);
            //将每个商品合计计入总计
            total = total + totalPrices;
            //在列表框中显示信息
            lbxList.Items.Add("单价:" + txtPrice.Text + "  数量:" + txtNum.Text + "  合计:" + totalPrices.ToString());
            //在lblTotalShow标签上显示总计数
            lblTotalShow.Text = total.ToString();
        }

  需求又来了,现在要求商场对商品搞活动,所有的商品打 8 折。扩展功能加了一个下拉选择框……v1.1版本来了

 

 

 

  核心代码(v1.1)

namespace ExtendDiscount
{
    public partial class frmMain :Skin_Metro
    {
        public frmMain()
        {
            InitializeComponent();
        }
        //声明一个double变量total来计算总计
        double total = 0.0d;
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //声明一个double变量totalPrices
            double totalPrices = 0d;
            //加入打折情况
            switch (cbxType.SelectedIndex)
            {
                case 0:
                    totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text);
                    break;
                case 1:
                    totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.8;
                    break;
                case 2:
                    totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.7;
                    break;
                case 3:
                    totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.5;
                    break;
            }
            //将每个商品合计计入总计
            total = total + totalPrices;
            //在列表框中显示信息
            lbxList.Items.Add("单价:" + txtPrice.Text + "  数量:" + txtNum.Text + "  合计:" + totalPrices.ToString());
            //在lblTotalShow标签上显示总计数
            lblTotalShow.Text = total.ToString();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cbxType.SelectedIndex = 0;
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            //数字0~9所对应的keychar为48~57
            e.Handled = true;
            //输入0-9
            if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
            {
                e.Handled = false;
            } 
        }

        private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            //数字0~9所对应的keychar为48~57
            e.Handled = true;
            //输入0-9
            if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
            {
                e.Handled = false;
            } 
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            total = 0.0;
            txtPrice.Text = "";
            txtNum.Text = "";
            lblTotalShow.Text = "";
            lbxList.Items.Clear();
            cbxType.SelectedIndex = 0;
        }
    }
}

 

  简单的工厂模式在WinForm中的应用

  现在又根据市场情况,添加此类需求——“满300送100、满200送50等等“

   面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类,打一折和打九折只是形式的不同,抽象分析出来,所有的打折算法都是一样的,所以打折算法应该是一个类;而”满300送100“等等,返利算法应该是另一个类。

  

 

 

 

  代码(v1.2)

  

namespace ExtendDiscountOfSimpleFactoryPattern
{
    //现金收取父类
    abstract class CashSuper
    {
        //抽象方法:收取现金,参数为原价,返回为当前价
        public abstract double acceptCash(double money);
    }
}
View Code
namespace ExtendDiscountOfSimpleFactoryPattern
{
    //正常消费,继承CashSuper
    class CashNormal:CashSuper
    {
        public override double acceptCash(double money)
        {
            return money;
        }
    }
}
View Code
namespace ExtendDiscountOfSimpleFactoryPattern
{
    //打折收费消费,继承CashSuper
    class CashRebate:CashSuper
    {
        private double moneyRebate = 1d;
        //初始化时,必需要输入折扣率,如八折,就是0,8
        public CashRebate(string moneyRebate)
        {
            //界面向类传值
            this.moneyRebate = double.Parse(moneyRebate);
        }
        public override double acceptCash(double money)
        {
            return money * moneyRebate;
        }
    }
}
View Code
namespace ExtendDiscountOfSimpleFactoryPattern
{
    //返利收费
    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;
        }
    }
}
View Code

  //简单工厂模式类

 1 namespace ExtendDiscountOfSimpleFactoryPattern
 2 {
 3     //收费对象生成工厂
 4     class CashFactory
 5     {
 6         //根据条件返回相应的对象
 7         public static CashSuper createCashAccept(string type)
 8         {
 9             CashSuper cs = null;
10             switch (type)
11             {
12                 case "正常消费":
13                     cs = new CashNormal();
14                     break;
15                 case "满300返100":
16                     cs = new CashReturn("300", "100");
17                     break;
18                 case "打8折":
19                     cs = new CashRebate("0.8");
20                     break;
21                 case "打7折":
22                     cs = new CashRebate("0.7");
23                     break;
24                 case "打5折":
25                     cs = new CashRebate("0.5");
26                     break;
27             }
28             return cs;
29         }
30     }
31 }

  //在WinForm中调用

  

 1 namespace ExtendDiscountOfSimpleFactoryPattern
 2 {
 3     public partial class frmMain :Skin_Metro
 4     {
 5         public frmMain()
 6         {
 7             InitializeComponent();
 8         }
 9         //客户端窗体程序
10         CashSuper cSuper;//声明一个父类对象
11 
12         //声明一个double变量total来计算总计
13         double total = 0.0d;
14         private void btnConfirm_Click(object sender, EventArgs e)
15         {
16             //声明一个double变量totalPrices
17             double totalPrices = 0d;
18            //利用简单工厂模式根据下拉选择框,生成相应的对象
19             cSuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
20             totalPrices = cSuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
21             //将每个商品合计计入总计
22             total = total + totalPrices;
23             //在列表框中显示信息
24             lbxList.Items.Add("单价:" + txtPrice.Text + "  数量:" + txtNum.Text + "  合计:" + totalPrices.ToString());
25             //在lblTotalShow标签上显示总计数
26             lblTotalShow.Text = total.ToString();
27         }
28 
29         private void btnReset_Click(object sender, EventArgs e)
30         {
31             total = 0.0;
32             txtPrice.Text = "";
33             txtNum.Text = "";
34             lblTotalShow.Text = "";
35             lbxList.Items.Clear();
36             cbxType.SelectedIndex = 0;
37         }
38 
39         private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
40         {
41             //数字0~9所对应的keychar为48~57
42             e.Handled = true;
43             //输入0-9
44             if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
45             {
46                 e.Handled = false;
47             } 
48         }
49 
50         private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
51         {
52             //数字0~9所对应的keychar为48~57
53             e.Handled = true;
54             //输入0-9
55             if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
56             {
57                 e.Handled = false;
58             } 
59         }
60 
61         private void frmMain_Load(object sender, EventArgs e)
62         {
63             //在窗体加载的时候,下拉选择框,就选择索引为0的元素——"正常消费"
64             cbxType.SelectedIndex = 0;
65         }
66     }
67 }

 

posted on 2020-07-10 21:41  笠侹凯树  阅读(262)  评论(0编辑  收藏  举报

导航