设计模式之☞策略模式

  策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。

  今天来通过一个案例,来讲一下商场的促销案例。一般商场会有那些活动呢?总结了下,一般会有这3种促销活动:1、正常收费;2、打折;3、满多少返多少

  面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的抽象集合才是类。

现金收费抽象类:

1     abstract class CashSupper //现金收费抽象类
2     {
3         public abstract double acceptCash(double money);//现金收取类的抽象方法,收取现金,参数为原价,返回为当前价
4     }
View Code

正常收费子类:

1     class CashNormal : CashSupper //正常收费子类
2     {
3         public override double acceptCash(double money)
4         {
5             return money;
6         }
7     }
View Code

打折收费子类:

 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     }
View Code

返利收费子类:

 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     }
View Code

现金收费工厂类:

 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     }
View Code

CashContext类:

 1  class CashContext
 2     {
 3         private CashSupper cs; //声明一个CashSuper
 4         public CashContext(CashSupper csuper) //通过构造方法,传入具体的收费策略
 5         {
 6             this.cs = csuper;
 7         }
 8         public double GetResult(double Money)
 9         {
10             return cs.acceptCash(Money); //根据收费策略的不同,获得计算结果
11         }
12     }
View Code

界面:

调用:

 1  double total = 0.0d; //用户总计
 2         private void btnOk_Click(object sender, EventArgs e)
 3         {
 4             CashContext cc = null;
 5             switch (cmbType.SelectedItem.ToString())
 6             {
 7                 case "正常收费":
 8                     cc = new CashContext(new CashNormal());
 9                     break;
10                 case "满300返100":
11                     cc = new CashContext(new CashReturn("300","100"));
12                     break;
13                 case "打9折":
14                     cc = new CashContext(new CashRebate("0.8"));
15                     break;
16             }
17             double totalPrices = 0d;
18             totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNumber.Text)); //通过对Context的GetResult方法的调用,可以得到收取费用的结果,让具体算法与客户进行隔离
19             total = total + totalPrices;
20             listBox1.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNumber.Text+" "+cmbType.SelectedItem+"合计:"+totalPrices.ToString());
21             label5.Text = total.ToString();
22         }
View Code

猜你喜欢

转载自www.cnblogs.com/chenyanbin/p/10202046.html
今日推荐