策略模式

<image>

在计算机编程中，策略模式（strategy pattern，也叫做policy pattern），是一种行为型软件设计模式，它确使可以在运行时选择算法。不再直接的实现一个单一的算法，代码接收运行时指令，选择一个算法家族中的某个算法。

## 概述

策略模式定义了一族可互换代替的算法（业务规则），并封装了每个算法。

## 结构

<image>

在上面的UML类图中，`Context`类不直接实现算法。`Context`转而提及`Strategy`接口来实行一个算法（`strategy.algorithm()`），这使得`Context`独立于算法是如何是实现的。`Strategy1`和`Strategy2`类实现了`Strategy`接口，就是说实现（封装）了一个算法。

UML序列图展示运行时交互：`Context`对象委托一个算法至不同的`Strategy`对象。首先`Context`调用`algorithm()`于`Strategy1`对象之上，它实行算法并将结果返回给`Context`。此后，`Context`变更它的策略并调用`algorithm()`在`Strategy2`对象之上，它实行一个算法并将结果返回给`Context`。

## 示例

### C++

下面是C++的例子：

```cpp
#include <iostream>

using namespace std;

class StrategyInterface {
public:
    virtual void execute() = 0;
};

class ConcreteStrategyA: public StrategyInterface {
public:
    virtual void execute() {
        cout << "Called ConcreteStrategyA execute method" << endl;
    }
};

class ConcreteStrategyB: public StrategyInterface {
public:
    virtual void execute() {
        cout << "Called ConcreteStrategyB execute method" << endl;
    }
};

class Context {
private:
    StrategyInterface *_strategy;

public:
    Context(StrategyInterface *strategy):_strategy(strategy) {
    }

    void set_strategy(StrategyInterface *strategy) {
        _strategy = strategy;
    }

    void execute() {
        _strategy->execute();
    }
};

int main(int argc, char *argv[])
{
    ConcreteStrategyA concreteStrategyA;
    ConcreteStrategyB concreteStrategyB;

    Context contextA(&concreteStrategyA);
    Context contextB(&concreteStrategyB);

    contextA.execute();
    contextB.execute();
    
    contextA.set_strategy(&concreteStrategyB);
    contextA.execute();

    return 0;
}
```

### Java

下面是Java的例子：

```java
// The classes that implement a concrete strategy should implement this
// The context class uses this to call the concrete strategy
interface Strategy {
    void execute();
}

// Implements the algorithm using the strategy interface
class FirstStrategy implements Strategy {
    public void execute() {
        System.out.println("Called FirstStrategy.execute()");
    }
}

class SecondStrategy implements Strategy {
    public void execute() {
        System.out.println("Called SecondStrategy.execute()");
    }
}

// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
class Context {
    Strategy strategy;

    // Constructor
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void execute() {
        this.strategy.execute();
    }
}

//StrategyExample test application
class StrategyExample {
    public static void main(String[] args) {
        Context context;

        // Three contexts following different strategies
        context = new Context(new FirstStrategy());
        context.execute();

        context = new Context(new SecondStrategy());
        context.execute();
    }
}
```

### C#

下面是C#的例子：

```csharp
using System;

namespace Wikipedia.Patterns.Strategy {
    // The classes that implement a concrete strategy should implement this
    // The context class uses this to call the concrete strategy
    interface IStrategy {
        void Execute();
    }

    // Implements the algorithm using the strategy interface
    class ConcreteStrategyA : IStrategy {
        public void Execute() {
            Console.WriteLine( "Called ConcreteStrategyA.Execute()" );
        }
    }

    class ConcreteStrategyB : IStrategy {
        public void Execute() {
            Console.WriteLine( "Called ConcreteStrategyB.Execute()" );
        }
    }

    // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
    class Context {
        IStrategy strategy;

        // Constructor
        public Context(IStrategy strategy) {
            this.strategy = strategy;
        }

        public void Execute() {
            strategy.Execute();
        }
    }

    // MainApp test application
    class MainApp {
        static void Main() {
            Context context;

            // Three contexts following different strategies
            context = new Context(new ConcreteStrategyA());
            context.Execute();

            context = new Context(new ConcreteStrategyB());
            context.Execute();
        }
    }
}
```

### Python

下面是Python的例子：

```python
from abc import ABC, abstractmethod

class Strategy(ABC):
    @abstractmethod
    def __call__(self): pass

class ConcreteStrategyA(Strategy):
    def __call__(self): 
        print("Called ConcreteStrategyA execute method")

class ConcreteStrategyB(Strategy):
    def __call__(self): 
        print("Called ConcreteStrategyB execute method")

class Context():
    def __init__(self, strategy):
        self.strategy = strategy
    def __call__(self):
        self.strategy()
```

其执行：

```pycon
>>> context = Context(ConcreteStrategyA())
>>> context()
Called ConcreteStrategyA execute method
>>> context = Context(ConcreteStrategyB())
>>> context()
Called ConcreteStrategyB execute method
```