策略模式
在上一篇文章中介绍了简单工厂模式,一个简单的银行系统开始初现端倪,现在我们开始为银行增加一个货币兑换业务,我们只需增加一个业务类,并实现IOperation接口就行了:
1 /// <summary>
2 /// 汇兑业务
3 /// </summary>
4 class Exchange : IOperation
5 {
6 public void operation()
7 {
8 Console.WriteLine("you are exchanging!");
9 }
10 }
2 /// 汇兑业务
3 /// </summary>
4 class Exchange : IOperation
5 {
6 public void operation()
7 {
8 Console.WriteLine("you are exchanging!");
9 }
10 }
然后再在简单工厂类中加上一个判断:
1 switch(operation)
2 {
3 case "turn":
4 opera = new TurningBox();
5 break;
6 case "out":
7 opera = new OutingBox();
8 break;
9 case "exchange":
10 opera = new Exchange();//汇兑业务
11 break;
12 default:
13 opera = new SavingBox();
14 break;
15 }
2 {
3 case "turn":
4 opera = new TurningBox();
5 break;
6 case "out":
7 opera = new OutingBox();
8 break;
9 case "exchange":
10 opera = new Exchange();//汇兑业务
11 break;
12 default:
13 opera = new SavingBox();
14 break;
15 }
也就是说我们每为银行增加一个业务都要重复以上两步操作,很明显,在银行业务很多的情况下,如缴费、汇款、理财等,用户每提出一个需求,简单工厂类都会通过switch来判定应该初始化哪一个业务类的执行效率是极其低下的,这为我们提出了新的挑战,就是策略模式。
单从用户这方面来考虑,如果用户手里有一笔钱,用户可以用来存款、也可以用来缴费、还可以用来转账;也就是说,用户有不同的选择方式,我们把用户的选择看成是策略,用户应该执行哪一种策略呢?
把简单工厂模式中的程序演化一下:
1 interface IStrategy
2 {
3 void operation();
4 }
5 /// <summary>
6 /// 汇兑
7 /// </summary>
8 class ExchangeStrategy : IStrategy
9 {
10 public void operation()
11 {
12 Console.WriteLine("you are exchanging!");
13 }
14 }
15 /// <summary>
16 /// 转账
17 /// </summary>
18 class TurningBoxStrategy : IStrategy
19 {
20 public void operation()
21 {
22 Console.WriteLine("you are turning!");
23 }
24 }
25 /// <summary>
26 /// 取款
27 /// </summary>
28 class OutingBoxStrategy : IStrategy
29 {
30 public void operation()
31 {
32 Console.WriteLine("you are outing!");
33 }
34 }
35 /// <summary>
36 /// 储存
37 /// </summary>
38 class SavingBoxStrategy : IStrategy
39 {
40 public void operation()
41 {
42 Console.WriteLine("you are saving!");
43 }
44 }
然后再增加一个用户选择策略类:
2 {
3 void operation();
4 }
5 /// <summary>
6 /// 汇兑
7 /// </summary>
8 class ExchangeStrategy : IStrategy
9 {
10 public void operation()
11 {
12 Console.WriteLine("you are exchanging!");
13 }
14 }
15 /// <summary>
16 /// 转账
17 /// </summary>
18 class TurningBoxStrategy : IStrategy
19 {
20 public void operation()
21 {
22 Console.WriteLine("you are turning!");
23 }
24 }
25 /// <summary>
26 /// 取款
27 /// </summary>
28 class OutingBoxStrategy : IStrategy
29 {
30 public void operation()
31 {
32 Console.WriteLine("you are outing!");
33 }
34 }
35 /// <summary>
36 /// 储存
37 /// </summary>
38 class SavingBoxStrategy : IStrategy
39 {
40 public void operation()
41 {
42 Console.WriteLine("you are saving!");
43 }
44 }
1 class ClientChoose
2 {
3 private IStrategy strategy;
4 public ClientChoose(IStrategy strategy)
5 {
6 this.strategy = strategy;
7 }
8 public void operation()
9 {
10 strategy.operation();
11 }
12 }
2 {
3 private IStrategy strategy;
4 public ClientChoose(IStrategy strategy)
5 {
6 this.strategy = strategy;
7 }
8 public void operation()
9 {
10 strategy.operation();
11 }
12 }
OK,当一个要求汇兑的用户来到银行时,先应选择策略,然后执行操作:
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 ClientChoose ExChangeClient = new ClientChoose(new ExchangeStrategy());
6 ExChangeClient.operation();
7 Console.ReadKey();
8 }
9 }
2 {
3 static void Main(string[] args)
4 {
5 ClientChoose ExChangeClient = new ClientChoose(new ExchangeStrategy());
6 ExChangeClient.operation();
7 Console.ReadKey();
8 }
9 }
策略模式与简单工厂模式的区别:
1.策略模式增加了客户端的主动性,客户自行选择策略,但智能化程度低下,因为客户必须首先了解银行提供了哪些服务,这些服务分别是什么?
2.简单工厂模式适合于用户的选择有限的情况,当用户选择较大时,通过服务器端的判断会大大增加服务器的负载;
3.一些情况下,将简单工厂模式和策略模式混合使用效果更好。