策略模式

策略模式的定义:定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它们的客户而变化。

我们来考虑这样一个现实场景,一个商场会有很多打折策略,如:对普通客户或全新客户全价;对老客户打5折;对大客户打10折等。如果我们来用程序实现,大多数人可能会用IF...ELSEIF...ELSE这样的结构来实现。但可能商场会增加其他打折策略,这样就会再来修改这段程序增加新的策略。但如果商场在促销的时候对所有人打3折,促销结束后再恢复本来的策略,这样通过反复修改上述代码显然不是一个好的办法。像这种通过IF...ELSE...来选择不同算法的程序,这些算法是具有平等地位的,是相互独立的,彼此之间没有依赖的,这些算法是可以彼此替换的,它们具有相同行为不同实现。我们可以用策略模式来实现。我们来看一下它的结构图:

我们来看一下client里的代码:

public class Client
{
   public static void Main(string[] args)
   {
        //这里选择了一个策略(算法)
        Strategy strategy = new StrategyTwo();
        //把选择的算法设置在上下文里
        PriceContext context = new PriceContext(strategy);
        //调用上下文的quote方法,实际上在quote方法里会转调具体策略的calcPrice方法
        double quote  =  context.quote(1000);
   }
}

 这里可能会有朋友问,如果这些算法需要的参数不一样怎么办,也就是说不一定可以抽象出来一个接口方法calcPrice(double goodPrice),这里有两种解决方案,一种是通过在策略类的构造函数里增加额外的参数,例如打折策略一可能需要增加一个客户身份来判断打折额度:

public class StrategyOne:Strategy
{
    private string customer;
    public StrategyOne(string customer)
    { 
        customer=customer;
    }
    
    public double CalcPrice(double goodsPrice)
    { 
        customer...
        goodsPrice...
    }
}

 还有一种方法是通过上下文来做手脚,

public class PriceContext
{
    public string customer{get; set;}
    public double goodsPrice{get; set;}
    private Strategy strategy;
    
    public PriceContext(Strategy strategy)
    {
          strategy=strategy;
    } 
    public double quote()
    {
         return strategy.CalcPrice(this);
    }
}

public class StrategyOne:Strategy
{    
    public double CalcPrice(PriceContext context)
    { 
        context.customer...
        context.goodsPrice...
    }
}

 有时候,策略模式还可以和模板方法模式结合起来,比如说,这几个打折算法的框架是相同的,只是在某几个步骤上不同,我们完全可以在策略模式的基础上加上模板方法模式:

 

策略模式的本质:分离算法,选择实现。

posted @ 2015-08-01 14:39  何塞穆里尼奥  阅读(335)  评论(0编辑  收藏  举报