设计模式学习笔记二:Strategy,策略模式
以下纯属学习笔记,希望各位高手指出不当之处。
在个人的使用个学习过程中感觉Strategy策略模式的主要功能就是用来封装各种业务规则(在需求分析的时候如果听到在不同的时候需要应用不同的业务规则),就可以考虑使用策略模式处理这种变化的可能性,它主要的代码实现过程如下
首先创建一个(1)抽象类,并且在抽象类中编写一个抽象构造函数用于被以后的各种规则类继承,然后编写(2)规则类来继承父抽象类,并且实现他的抽象构造函数,再开发一个(3)上下文类其功能是承接前台文件和后台规则类的使用。
以下具体代码是一个商场价格计算器的源代码,使用了Strategy策略模式的编程思想
(1)策略模式抽象类父类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 策略模式抽象类父类
3 /// </summary>
4 abstract class Strategy
5 {
6 //封装方法
7 public abstract double AlgorithmInterface(double money);
8 }
2 /// 策略模式抽象类父类
3 /// </summary>
4 abstract class Strategy
5 {
6 //封装方法
7 public abstract double AlgorithmInterface(double money);
8 }
(2) 各种封装好的子类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 正常收费
3 /// </summary>
4 class CashRebate : Strategy
5 {
6 public override double AlgorithmInterface(double money)
7 {
8 return money;
9 }
10 }
11
12 /// <summary>
13 /// 打折收费子类
14 /// </summary>
15 class CashBate : Strategy
16 {
17 private double moneyRebate = 1d;
18 public CashBate(string moneyRebate)
19 {
20 this.moneyRebate = double.Parse(moneyRebate);
21 }
22
23 public override double AlgorithmInterface(double money)
24 {
25 return money * moneyRebate;
26 }
27 }
28
29 /// <summary>
30 /// 返利收费类
31 /// </summary>
32 class CashReturn : Strategy
33 {
34 private double moneyCondition = 0.0d;
35 private double moneyReturn = 0.0d;
36
37 /// <summary>
38 /// 返利条件
39 /// </summary>
40 /// <param name="moneyCondition">需要满足的金额</param>
41 /// <param name="moneyReturn">返利金额</param>
42 public CashReturn(string moneyCondition, string moneyReturn)
43 {
44 this.moneyCondition = double.Parse(moneyCondition);
45 this.moneyReturn = double.Parse(moneyReturn);
46 }
47
48 public override double AlgorithmInterface(double money)
49 {
50 double result = money;
51 if (money >= moneyCondition)
52 {
53 result = money - Math.Floor(money / moneyCondition) * moneyReturn;
54 }
55 return result;
56 }
57 }
2 /// 正常收费
3 /// </summary>
4 class CashRebate : Strategy
5 {
6 public override double AlgorithmInterface(double money)
7 {
8 return money;
9 }
10 }
11
12 /// <summary>
13 /// 打折收费子类
14 /// </summary>
15 class CashBate : Strategy
16 {
17 private double moneyRebate = 1d;
18 public CashBate(string moneyRebate)
19 {
20 this.moneyRebate = double.Parse(moneyRebate);
21 }
22
23 public override double AlgorithmInterface(double money)
24 {
25 return money * moneyRebate;
26 }
27 }
28
29 /// <summary>
30 /// 返利收费类
31 /// </summary>
32 class CashReturn : Strategy
33 {
34 private double moneyCondition = 0.0d;
35 private double moneyReturn = 0.0d;
36
37 /// <summary>
38 /// 返利条件
39 /// </summary>
40 /// <param name="moneyCondition">需要满足的金额</param>
41 /// <param name="moneyReturn">返利金额</param>
42 public CashReturn(string moneyCondition, string moneyReturn)
43 {
44 this.moneyCondition = double.Parse(moneyCondition);
45 this.moneyReturn = double.Parse(moneyReturn);
46 }
47
48 public override double AlgorithmInterface(double money)
49 {
50 double result = money;
51 if (money >= moneyCondition)
52 {
53 result = money - Math.Floor(money / moneyCondition) * moneyReturn;
54 }
55 return result;
56 }
57 }
(3)上下文类,承担中间件的工作,让前台无压力变更
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 上下文类
3 /// </summary>
4 class CashContext
5 {
6 Strategy st = null;
7
8 public CashContext(string type)
9 {
10 switch (type)
11 {
12 case "正常收费":
13 CashRebate cr = new CashRebate();
14 st = cr;
15 break;
16 case "满300返100":
17 CashReturn cr0 = new CashReturn("300", "100");
18 st = cr0;
19 break;
20 case "打8折"
21 CashBate cb = new CashBate("0.8");
22 st = cb;
23 break;
24 }
25 }
26
27 public double GetResult(double money)
28 {
29 return st.AlgorithmInterface(money);
30 }
31 }
2 /// 上下文类
3 /// </summary>
4 class CashContext
5 {
6 Strategy st = null;
7
8 public CashContext(string type)
9 {
10 switch (type)
11 {
12 case "正常收费":
13 CashRebate cr = new CashRebate();
14 st = cr;
15 break;
16 case "满300返100":
17 CashReturn cr0 = new CashReturn("300", "100");
18 st = cr0;
19 break;
20 case "打8折"
21 CashBate cb = new CashBate("0.8");
22 st = cb;
23 break;
24 }
25 }
26
27 public double GetResult(double money)
28 {
29 return st.AlgorithmInterface(money);
30 }
31 }
(4)前台调用方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void StrategyTest_Click(object sneder, EventArgs e)
{
CashContext cc = new CashContext(ddlType.SelectedItem.ToString()); //根据下拉列表的值来进行抽象类的实现
double totalPrices = 0d;
totalPrices = cc.GetResult(1000);
TextBox3.Text = totalPrices.ToString();
}
{
CashContext cc = new CashContext(ddlType.SelectedItem.ToString()); //根据下拉列表的值来进行抽象类的实现
double totalPrices = 0d;
totalPrices = cc.GetResult(1000);
TextBox3.Text = totalPrices.ToString();
}
在实践的过程中证明策略模式几乎可以用来封装任何类型的规则,在开发过程中,一个业务流程当条件不同的时候需要应用多种业务规则,那么这种需求我们就可以使用策略模式,以上的代码之是非常简单的示例,可以充分的进行扩展然后使用,它封装了各种的算法,并且当业务规则进行变更的时候只要修改封装类就可以了。