private void btnOk_Click(object sender, EventArgs e) { CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = csuper.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(); }
private static void NewMethod() { //input 3 parameter var cs = CashFactory.CreateCashAccept("300 minus 100"); const double txtPrice = 600; const int txtNum = 1; var totalPrice = cs.GetResult(txtPrice) * txtNum; Console.WriteLine("TotalPrice " + totalPrice); Console.ReadLine(); }
static void Main(string[] args) {//還是工廠 CashFactory CashSuper會看到2個類別 double total = 0d; CashSuper cs = CashFactory.createCashAccept("打8折"); double totalprice = 0d; totalprice = cs.acceptCash(5000d) * 5;//price*num total += totalprice; Console.WriteLine(total); //策略模式 只需要一個CashContext類別 收費演算法跟用戶端分離 CashContext cc = null; cc = new CashContext("打8折"); totalprice = 0d; totalprice = cc.GetResult(5000) * 5; //單價X數量 total += totalprice; Console.WriteLine(total); Console.ReadKey(); }