标签:context math 返回 F12 factor 计算 opened 方法 分类
策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。
今天来通过一个案例,来讲一下商场的促销案例。一般商场会有那些活动呢?总结了下,一般会有这3种促销活动:1、正常收费;2、打折;3、满多少返多少
面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的抽象集合才是类。
现金收费抽象类:
1 abstract class CashSupper //现金收费抽象类 2 { 3 public abstract double acceptCash(double money);//现金收取类的抽象方法,收取现金,参数为原价,返回为当前价 4 }
正常收费子类:
1 class CashNormal : CashSupper //正常收费子类 2 { 3 public override double acceptCash(double money) 4 { 5 return money; 6 } 7 }
打折收费子类:
1 class CashRebate : CashSupper //打折收费子类 2 { 3 private double moneryRebate = 1d; 4 public CashRebate(string moneryRebate) 5 { 6 this.moneryRebate = double.Parse(moneryRebate); //打折收费,初始化时,必须要输入折扣率,如八折,就是0.8 7 } 8 public override double acceptCash(double money) 9 { 10 return money * moneryRebate; 11 } 12 }
返利收费子类:
1 class CashReturn : CashSupper 2 { 3 private double moneryCondition = 0.0d; 4 private double MoneryReturn = 0.0d; 5 public CashReturn(string moneryCondition,string moneryReturn) //返利收费,初始化时必须要输入返利条件和返利值,比如满300返100,则moneryCondition=300,moneryReturn=100 6 { 7 this.moneryCondition =double.Parse(moneryCondition); 8 this.MoneryReturn = double.Parse(moneryReturn); 9 } 10 public override double acceptCash(double money) 11 { 12 double result = money; 13 if (money>=moneryCondition) //若大于返利条件,则需要减去返利值 14 { 15 result = money - Math.Floor(money / moneryCondition) * MoneryReturn; 16 } 17 return result; 18 } 19 }
现金收费工厂类:
1 class CashFactory 2 { 3 public static CashSupper createCashAccept(string type) 4 { 5 CashSupper cs = null; 6 switch (type) 7 { 8 case "正常收费": 9 cs = new CashNormal(); 10 break; 11 case "满300反100": 12 CashReturn cr1 = new CashReturn("300","100"); 13 cs = cr1; 14 break; 15 case "打8折": 16 CashRebate cr2 = new CashRebate("0.8"); 17 cs = cr2; 18 break; 19 } 20 return cs; 21 } 22 }
CashContext类:
1 class CashContext 2 { 3 private CashSupper cs=null; //声明一个CashSuper 4 public CashContext(string type) //表示收费的类型 5 { 6 switch (type) 7 { 8 case "正常收费": 9 CashNormal cs0 = new CashNormal(); 10 cs = cs0; 11 break; 12 case "满300返100": 13 CashReturn cr1 = new CashReturn("300","100"); 14 cs = cr1; 15 break; 16 case "打8折": 17 CashRebate cr2 = new CashRebate("0.8"); //将实例化具体策略的过程由客户端转移到Context类中。简单工厂的引用 18 cs = cr2; 19 break; 20 } 21 } 22 public double GetResult(double Money) 23 { 24 return cs.acceptCash(Money); //根据收费策略的不同,获得计算结果 25 } 26 }
界面:
调用:
1 double total = 0.0d; //用户总计 2 private void btnOk_Click(object sender, EventArgs e) 3 { 4 CashContext cc = new CashContext(cmbType.SelectedItem.ToString()); 5 double totalPrices = 0d; 6 totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNumber.Text)); //通过对Context的GetResult方法的调用,可以得到收取费用的结果,让具体算法与客户进行隔离 7 total = total + totalPrices; 8 listBox1.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNumber.Text+" "+cmbType.SelectedItem+"合计:"+totalPrices.ToString()); 9 label5.Text = total.ToString(); 10 }
标签:context math 返回 F12 factor 计算 opened 方法 分类
原文地址:https://www.cnblogs.com/chenyanbin/p/10202046.html