一、策略模式定义
参考Gang of Four's (GOF) Design Patterns书里的定义:Define a family of algorithms, encapsulate each one, and make them interchangeable. [The] Strategy [pattern] lets the algorithm vary independently from clients that use it
策略模式(Strategy)属于对象行为型设计模式,主要是定义算法族,分别封装起来,让他们之间可以项目替换,此模式让算法的变化独立于使用算法的客户
二、核心概念和用法
策略模式主要用到了三个核心的OOP概念:封装、继承和多态。还包含了一些基本的设计原则:1)封装变化。2)多用组合,少用继承。3)针对接口编程,不 针对实现编程。
此模式的适用场景是:当一个类的某些行为将会经常发生变化或者在运行时需要改变时,可以使用策略模式。
这个模式的核心是把类的变化行为提取为一个接口,然后将类的变化部分用这个接口对象代替,也即声明为这个接口的一个属性,然后增加一个设置这个接口的方 法,以便以后修改这个类的具体行为,这样这个类可以不用管这个变化行为的具体实现,将实现委托给这个接口的具体实现。而关于这个接口的一系列实现,可以看 成是一族算法。这样就实现了将类中变化的部分封装,算法的使用跟算法的实现分离,算法被提取出来之后还可以更好的进行复用。
三、模式参与者
下面快速浏览一下这个模式主要参与者:
- Context超类
定义了所有需要继承这个类的子类的属性跟方法,并且定义了改变这个父类变化行为 的方法
- 算法接口
定义了具体行为的方法声明,由这个接口的实现类来具体实现接口定义的行为方法。
- 具体算法类
一组具体的算法接口实现类,代表不同的行为。
- 客户类(Client Class)
负责创建Context类的实例,并负责创建算法的实现的实例,然后赋给 Context类实例的正确算法实现对象。
四、核心模式图表
策略模式diagram
五、一个简单的策略模式的实现
//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();
context = new Context(new ThirdStrategy());
context.execute();
}
}
// 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()");
}
}
class ThirdStrategy implements Strategy {
public void execute() {
System.out.println("Called ThirdStrategy.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();
}
}