您好,登錄后才能下訂單哦!
//收費策略Context class CashContext { //聲明一個現金收費父類對象 private CashSuper cs; //設置策略行為,參數為具體的現金收費子類(正常,打折或返利) public CashContext(CashSuper csuper) { this.cs = csuper; } //得到現金促銷計算結果(利用了多態機制,不同的策略行為導致不同的結果) public double GetResult(double money) { return cs.acceptCash(money); } }
View Code //現金收取工廠 class CashContext { CashSuper cs = null ; //根據條件返回相應的對象 public CashContext(string type) { switch (type) { case "正常收費" : CashNormal cs0 = new CashNormal(); cs = cs0; break; case "滿300返100" : CashReturn cr1 = new CashReturn( "300", "100" ); cs = cr1; break; case "打8折" : CashRebate cr2 = new CashRebate( "0.8"); cs = cr2; break; } } public double GetResult(double money) { return cs.acceptCash(money); } }
View Code private void btnOk_Click(object sender, EventArgs e) { 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 "打8折" : cc = new CashContext (new CashRebate("0.8" )); break; } double totalPrices = 0d; totalPrices = cc.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(); }
View Codeprivate void btnOk_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(); }
View CodeDataSet ds;//用于存放配置文件信息 double total = 0.0d;//用于總計 private void Form1_Load(object sender, EventArgs e) { //讀配置文件 ds = new DataSet(); ds.ReadXml(Application.StartupPath + "\\CashAcceptType.xml"); //將讀取到的記錄綁定到下拉列表框中 foreach (DataRowView dr in ds.Tables[0].DefaultView) { cbxType.Items.Add(dr["name"].ToString()); } cbxType.SelectedIndex = 0; } private void btnOk_Click(object sender, EventArgs e) { CashContext cc = new CashContext(); //根據用戶的選項,查詢用戶選擇項的相關行 DataRow dr = ((DataRow[])ds.Tables[0].Select("name='" + cbxType.SelectedItem.ToString()+"'"))[0]; //聲明一個參數的對象數組 object[] args =null; //若有參數,則將其分割成字符串數組,用于實例化時所用的參數 if (dr["para"].ToString() != "") args = dr["para"].ToString().Split(','); //通過反射實例化出相應的算法對象 cc.setBehavior(dr["class"].ToString(), args); double totalPrices = 0d; totalPrices = cc.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(); }
View Codeclass CashContext { private CashSuper cs; public void setBehavior(string className, object[] args) { this.cs = (CashSuper)Assembly.Load("商場管理軟件").CreateInstance("商場管理軟件." + className, false, BindingFlags.Default, null, args, null, null); } public double GetResult(double money) { return cs.acceptCash(money); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。