您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
//收费策略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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。