Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12471659
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2013-01-05 09:33:18

一、经典案例


设计一个商店收银系统。完成最基本的商品买卖收费统计问题。能解决各个商品正常情况下的收费处理\打折情况下的收费处理\返利情况下的收费处理。


image

图 UML图

抽象策略基类

点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     /* 注意抽象类的写法 */
  4.     abstract class Strategy
  5.     {
  6.         abstract public void ArithmeticInterface();
  7.     }
  8. }

三个派生类,继承相同的公共方法进行不同的实现。

点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     class ConcreteStrategyA : Strategy
  4.     {
  5.         public override void ArithmeticInterface()
  6.         {
  7.             Console.WriteLine("Operate in ConcreteStategyA");
  8.             //throw new NotImplementedException();
  9.         }
  10.     }
  11. }


点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     class ConcreteStrategyB : Strategy
  4.     {
  5.         public override void ArithmeticInterface()
  6.         {
  7.             Console.WriteLine("Operate in ConcreteStrategyB");
  8.             //throw new NotImplementedException();
  9.         }
  10.     }
  11. }


点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     class ConcreteStrategyC : Strategy
  4.     {
  5.         public override void ArithmeticInterface()
  6.         {
  7.             Console.WriteLine("Operate in ConcreteStrategyC");
  8.             //throw new NotImplementedException();
  9.         }
  10.     }
  11. }

 

关键步骤,通过策略上下文类来调用各个派生类的实例!

点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     class StrategyContext
  4.     {
  5.         public Strategy strategy;

  6.         public StrategyContext(Strategy strategy)
  7.         {
  8.             this.strategy = strategy;
  9.         }

  10.         public void Invoke()
  11.         {
  12.             this.strategy.ArithmeticInterface();
  13.         }
  14.     }
  15. }


点击(此处)折叠或打开

  1. namespace StrategyPattern
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             StrategyContext strategyContext;

  8.             strategyContext = new StrategyContext(new ConcreteStrategyA());
  9.             strategyContext.Invoke();
  10.             strategyContext = new StrategyContext(new ConcreteStrategyB());
  11.             strategyContext.Invoke();
  12.             strategyContext = new StrategyContext(new ConcreteStrategyC());
  13.             strategyContext.Invoke();

  14.             Console.ReadLine();
  15.         }
  16.     }
  17. }


image


StrategyPattern.zip  

策略模式原始案例.zip  

 

二、策略模式的理论引出 


1、策略模式的概念

策略模式是一种定义一系列算法的的方法,这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了了各种算法类与使用算法类之间的耦合。


2、策略模式的优点

上面案例中,策略模式的Strategy类层次为StrategyContext定义了一系列的可供重用的算法或行为。继承有助于取出这些算法的中的公共功能。

另外,策略模式简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。

算法类间相对独立,修改其中任一个不会影响其他的算法。


3、策略模式的用途

策略模式就是用来封闭算法的,在实践中可以用它来封装几乎任何类型的的规则,只要在分析过程中听到需要在不同时间应用不同的业务规则,就可考虑使用策略模式处理这种变化的可能性。


三、参考文献

《大话设计模式》.程杰著

阅读(2252) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~