Esempio n. 1
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (this.txtPrice.Text.Trim() == "" || this.txtNumber.Text.Trim() == "")
            {
                MessageBox.Show("Please input the price and number!");
                return;
            }

            CashContext cashContext;
            double      cashFinal = 0;

            switch (Convert.ToInt32(this.cboDiscount.SelectedValue))
            {
            case 1:
                cashContext = new CashContext(new CashNoDiscount());
                cashFinal   = cashContext.GetCashResultFinal(Convert.ToDouble(this.txtPrice.Text.Trim()),
                                                             Convert.ToDouble(this.txtNumber.Text.Trim()));
                break;

            case 2:
                cashContext = new CashContext(new CashPercent());
                cashFinal   = cashContext.GetCashResultFinal(Convert.ToDouble(this.txtPrice.Text.Trim()),
                                                             Convert.ToDouble(this.txtNumber.Text.Trim()));
                break;

            case 3:
                cashContext = new CashContext(new CashReturn());
                cashFinal   = cashContext.GetCashResultFinal(Convert.ToDouble(this.txtPrice.Text.Trim()),
                                                             Convert.ToDouble(this.txtNumber.Text.Trim()));
                break;
            }

            this.txtResult.Text = "Total : " + cashFinal.ToString();
        }
Esempio n. 2
0
        // 策略模式 定义一系列算法,把它们一个个封装起来,并且使它们之间可互相替换,从而让算法可以独立于使用它的用户而变化。
        static void Strategy()
        {
            CashNormal  cashNormal    = new CashNormal();
            CashContext contextNormal = new CashContext(cashNormal);

            Console.WriteLine("700:" + contextNormal.GetResult(700));

            CashRebate  cashRebate    = new CashRebate("0.8");
            CashContext contextRebate = new CashContext(cashRebate);

            Console.WriteLine("700打8折:" + contextRebate.GetResult(700));

            CashReturn  cashReturn    = new CashReturn("300", "100");
            CashContext contextReturn = new CashContext(cashReturn);

            Console.WriteLine("700满300减100:" + contextReturn.GetResult(700));
        }