static void Main(string[] args) { double money = 0; CashContext context; money = Convert.ToDouble(Console.ReadLine()); CashSuper cashNormal = new CashNormal(); context = new CashContext(cashNormal); Console.WriteLine("无任何活动下收费:" + context.GetResult(money)); CashSuper cashRebate = new CashRebate(0.8); context = new CashContext(cashRebate); Console.WriteLine("打八折的情况下收费:" + context.GetResult(money)); CashSuper cashReturn = new CashReturn(300, 100); context = new CashContext(cashReturn); Console.WriteLine("满300送100下收费:" + context.GetResult(money)); context = new CashContext(new List <CashSuper> { cashRebate, cashReturn }); Console.WriteLine("先打八折,再满300 送100下收费:" + context.GetResult(money)); context = new CashContext(new List <CashSuper> { cashReturn, cashRebate }); Console.WriteLine("先满300 送100,再打八折:" + context.GetResult(money)); Console.ReadKey(); }
private void BtnOk_OnClick(object sender, RoutedEventArgs e) { var cashContext = new CashContext(CbDiscount.SelectionBoxItem.ToString()); var totalPrice = cashContext.GetResult(Convert.ToDouble(TbPrice.Text) * Convert.ToDouble(TbCount.Text)); ListBoxSum.Items.Add("单价:" + TbPrice.Text + " 数量:" + TbCount.Text + " " + CbDiscount.SelectedIndex + " 合计:" + totalPrice.ToString()); _total += totalPrice; LbTotal.Content = _total.ToString(); }
private static void Strategy() { Console.WriteLine("请输入收费标准:"); string inputType = Console.ReadLine(); CashContext cashContext = new CashContext(inputType); double totalPrices = 0d, price = 130d, num = 3d; totalPrices = cashContext.GetResult(price * num); total += totalPrices; Console.WriteLine("单价为:{0},数量:{1},总价:{2}", price, num, total); }
static void Main(string[] args) { string type = string.Empty; double money = 0.0; Console.WriteLine("请输入打折方式:"); type = Console.ReadLine(); CashContext context = new CashContext(type); Console.WriteLine("请输入总金额:"); money = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("最后应该收费:" + context.GetResult(money)); Console.ReadKey(); }
private void btnOk_Click(object sender, EventArgs e) { try { var cashContext = new CashContext(cbbType.SelectedItem.ToString()); var totalPrice = cashContext.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtAmount.Text)); lbxList.Items.Add($"{cbbType.Text}:单价:{txtPrice.Text},数量:{txtAmount.Text},合计:{totalPrice}"); lblTotalPrice.Text = totalPrice.ToString(CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnOk_Click(object sender, EventArgs e) { try { var cashContext = new CashContext(cbbType.SelectedItem.ToString()); var totalPrice = cashContext.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtAmount.Text)); lbxList.Items.Add($"{cbbType.Text}:单价:{txtPrice.Text},数量:{txtAmount.Text},合计:{totalPrice}"); lblTotalPrice.Text = totalPrice.ToString(CultureInfo.InvariantCulture); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// [Strategy Pattern]:定義了演算法家族,並分別封裝,讓他們之間可以互相替換。此模式讓演算法的變化不會影響到使用演算法的客戶 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { // 原始策略模式 double totalPrice = 0.0d; double total = 0.0d; fCashContext = comboBox1.SelectedItem.ToString() switch { "Normal" => new CashContext(new CashNormal()), "Rebate" => new CashContext(new CashRebate(0.8)), "Return" => new CashContext(new CashReturn(300, 100)), _ => throw new Exception("不知名的外星人") }; totalPrice = fCashContext.GetResult(100); total += totalPrice; MessageBox.Show(total.ToString()); // strategy + factory fCashContext = new CashContext(comboBox1.SelectedItem.ToString(), 0.8, 300, 100); totalPrice = fCashContext.GetResult(100); // 簡單工廠用法 // ICash cash = CashFactory.CreateCashFactory(CashType.Normal); // ... = cash.GetResult(...); // 策略+簡單工廠 // fCashContext = new CashContext(comboBox1.SelectedItem.ToString(), 0.8, 300, 100); // ... = fCashContext.GetResult(...); // 只使用 [simple factory pattern] client端必須知道 ICash 和 CashFactory // 而 [ Strategy Pattern ] 只需認識CashContext,其餘動作皆封裝起來,"耦合性更加降低"。 // 策略模式的優點是簡化了unit test,因為每個演算法都有屬於自己的類別,可以透過自己的介面進行單獨測試 // (修改任一其中演算法並不會影響其他演算法) // 總結:[策略模式] 就是用來封裝演算法的,只要在分析過程中聽到需要在不同時間使用不同演算法就可以考慮使用策略模式處理這種變化的可能性 // 當不同行為堆砌在同類別就很難避免使用條件敘述來選擇合適的行為。 // 將這些行為封裝在一個個獨立的Strategy類別中,可以在使用這些行為中的類別消除條件敘述 // 以這個商場收銀為例子 (Client 最終消除了條件敘述 只需要輸入設定檔(type、滿額折扣、打折)參數就可以避免大量判斷 // 也就是說 [ 策略模式封裝了變化 ]。 // 而結合使用了策略+簡單工廠這兩種patterns後大大減輕了Client端的負擔。 // 網路上找到的解釋 個人認為蠻好理解的↓ // 簡單工廠模式是用來建立物件的模式,關注物件如何被產生 // 策略模式是一種行為模式,關注的是行為的封裝 } }
static void Main(string[] args) { Console.WriteLine("请输入单价"); string price = Console.ReadLine(); Console.WriteLine("请输入数量"); string num = Console.ReadLine(); Console.WriteLine("请输入打折方式"); string fuc = Console.ReadLine(); CashContext csuper = new CashContext(fuc); double totalPrices = csuper.GetResult(Convert.ToDouble(price) * Convert.ToDouble(num)); Console.WriteLine("单价:{0},数量:{1},合计:{2}", price, num, totalPrices); Console.ReadLine(); }
private void confirm_Click(object sender, EventArgs e) { try { CashContext cashContext = new CashContext(comboBox1.SelectedItem.ToString()); double totalPrice = 0.0d; totalPrice = cashContext.GetResult(Convert.ToDouble(textPrice.Text) * Convert.ToDouble(textAmo.Text)); totalMoney += totalPrice; listBox1.Items.Add("单价:" + textPrice.Text + " 数量:" + textAmo.Text + " 合计:" + totalPrice); total.Text = totalMoney.ToString(); } catch (Exception ex) { MessageBox.Show("Error" + ex.Message); } }
private void button1_Click(object sender, EventArgs e) { CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lbxList.Items.Add("单价: " + txtPrice.Text + " 数量: " + txtNum.Text + " " + cbxType.SelectedItem + " 合计: " + totalPrices.ToString()); lblResult.Text = total.ToString(); //CashSuper cs = CashFactory.CreateCashAccept(cbxType.SelectedItem.ToString()); //double totalPrices = 0d; //totalPrices = cs.acceptCash(Convert.ToDouble(txtPrice.Text) * // Convert.ToDouble(txtNum.Text)); //total = total + totalPrices; //lbxList.Items.Add("单价: " + txtPrice.Text + " 数量: " + txtNum.Text // + " " + cbxType.SelectedItem + " 合计: " + totalPrices.ToString()); //lblResult.Text = total.ToString(); }
public static void Main(string[] args) { #region Prototype var context = new Context(new ConcreteStrategyA()); context.Implement(); context = new Context(new ConcreteStrategyB()); context.Implement(); context = new Context(new ConcreteStrategyC()); context.Implement(); Console.WriteLine(); #endregion Prototype var stragetyArray = new[] { "8 折", "正常", "原价", "满300返100" }; var random = new Random(); for (var i = 0; i < 10; i++) { var cashContext = new CashContext(stragetyArray[random.Next(stragetyArray.Length)]); var money = random.Next(500); Console.WriteLine($"结算方式:{cashContext.StrategyType} ,原价{money},实际收取:{cashContext.GetResult(money)}"); } Console.ReadLine(); }