策略模式(Strategy Pattern)

摘自:

策略模式(Strategy):它定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策 略模式让算法的变化不会影响到使用算法的客户。(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)

图1 策略模式类图

 优点:

  1、 简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
  2、 避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。
      3、 遵守大部分GRASP原则和常用设计原则,高内聚、低偶合。

  缺点:
  1、 因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量。
      2、 在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象。(这本身没有解除客户端需要选择判断的压力,而策略 模式与简单工厂模式结合后,选择具体实现的职责也可以由Context来承担,这就最大化的减轻了客户端的压力。

代码:

    public abstract class Strategy
    {
        public abstract void strategyInterface();
    }

    public class ConcreteStrategy : Strategy
    {
        public override void strategyInterface()
        {
            Console.Write("ConcreteStrategy strategyInterface");
        } 
    }

    public class Context
    {
        private Strategy strategy = null;
        public Context(Strategy strtegy)
        {
            this.strategy = strategy;
        }
        public void contextInterface()
        {
            this.strategy.strategyInterface();
        }
    }

调用:

  Context context = new Context(new ConcreteStrategy());
  context.contextInterface();
原文地址:https://www.cnblogs.com/nygfcn1234/p/3406849.html