02、策略模式(Strategy)

一、概念:

策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,

手段不同而最终达成的目标一致。客户只关心目标而不在意具体的实现方法,

实现方法要根据具体的环境因素而变化。

二、案例思路

用我们每天上下班的方式来展开:

我们上下班的交通方式有:

  1、步行 

  2、骑直行车

  3、地铁 

  4、开车 

三、类图

                                

四、代码

1、首先我们声明策略类,父类Strategy,定义为抽象类,方法也为抽象方法,便于子类进行相关的实现。

1     public abstract class Strategy
2     {
3         public abstract void AtWalk();
4     }

 

2、声明几个上班方式的子类继承自父类Strategy

 1    //  1、汽车 2、步行 3、地铁  
 2 
 3     public class WalkStrategy:Strategy
 4     {
 5         public override void AtWalk()
 6         {
 7             Console.WriteLine("我是走路去上班的");
 8         }
 9     }
10 
11     public class SubwayStrategy:Strategy
12     {
13         public override void AtWalk()
14         {
15             Console.WriteLine("我是挤地铁的");
16         }
17     }
18 
19     public class CarStrateg : Strategy
20     {
21         public override void AtWalk()
22         {
23             Console.WriteLine("小康,开车上下班");
24         }
25     }

 

3、写出Context类上下文代码;

 1   //山下文
 2     public class Context
 3     {
 4         Strategy strategy = null;
 5         public Context (Strategy strategy)
 6         {
 7             this.strategy = strategy;
 8         }
 9         //上下文接口
10         public void ContextInterface()
11         {
12             strategy.AtWalk();
13         }
14     }

 

4、到这一步,策略模式的核心就已经完成了,接下来就可以对策略模式进行相关的测试了;

 1   //山下文
 2     public class Context
 3     {
 4         Strategy strategy = null;
 5         public Context (Strategy strategy)
 6         {
 7             this.strategy = strategy;
 8         }
 9         //上下文接口
10         public void ContextInterface()
11         {
12             strategy.AtWalk();
13         }
14     }

 

5、输出如图;

                            

 

6、这里再扩充下,让策略模式和简单工厂进行结合

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //注意,调用的时候被指定调用了,可以自己设置一个限制条件来限制客户的输入,希望读者可以自己去探索
14 
15             Context context = new Context("car");
16             context.ContextInterface();
17             Console.ReadKey();
18         }
19     }
20     public abstract class Strategy
21     {
22         public abstract void AtWalk();
23     }
24 
25     //  1、汽车 2、步行 3、地铁  
26 
27     public class WalkStrategy : Strategy
28     {
29         public override void AtWalk()
30         {
31             Console.WriteLine("我是走路去上班的");
32         }
33     }
34     public class SubwayStrategy : Strategy
35     {
36         public override void AtWalk()
37         {
38             Console.WriteLine("我是挤地铁的");
39         }
40     }
41     public class CarStrateg : Strategy
42     {
43         public override void AtWalk()
44         {
45             Console.WriteLine("小康,开车上下班");
46         }
47     }
48     //山下文
49     public class Context
50     {
51         Strategy strategy = null;
52         public Context(string name)
53         {
54             switch(name)
55             {
56                 case "car":
57                     strategy = new CarStrateg();
58                     break;
59                 case "walk":
60                     strategy = new WalkStrategy();
61                     break;
62                 case "subway":
63                     strategy = new SubwayStrategy();
64                     break;
65             }
66         }
67         //上下文接口
68         public void ContextInterface()
69         {
70             strategy.AtWalk();
71         }
72     }
73 }

 

五、总结:

  策略模式参与者:

   Strategy 策略:定义所支持的算法的公共接口。Context使用这个接口来调用某个Strategy子对象定义的算法。

   Strategy子对象具体策略:实现Strategy接口中的具体算法。

   Context 上下文

    1、 通过一个子对象来对其进行配置;

    2、 维护一个对Strategy对象的引用;

    3、 可定义一个接口来让Strategy访问它的数据。

 

六、案例推荐:

  1、上班方式

  2、空中飞行的东西

  3、旅行的出动方式

  4、彩票的出奖

  5、超市的优惠方式

posted @ 2017-12-05 17:25  Turtle_Zhang  阅读(413)  评论(0编辑  收藏  举报