在软件开发中,某一功能可以有多种实现,如果把这些实现都放在一个类中,则不利于增加、修改和替换,此时可以把可能频繁变化的逻辑抽象出来,Runnable
就采用了类似的思想。这种设计模式叫策略模式:
- 定义了一族算法(业务规则)
- 封装了每个算法
- 这族的算法可互换代替
策略模式相当于在面向对象层面实现函数式编程,于是对于 Python 等可以把方法作为参数的语言,策略模式就显得没必要了。Java 8 也引入了函数式编程的设计,例如 java.util.function
下的各种接口以及 lambda 表达式。
实例
抽象策略
public interface Strategy {
void execute();
}
具体策略
public class ConcreteStrategyA implements Strategy {
@Override
public void execute() {
System.out.println("executing " + this);
}
}
public class ConcreteStrategyB implements Strategy {
@Override
public void execute() {
System.out.println("executing " + this);
}
}
环境类
public class Context {
private Strategy mStrategy;
public Context(Strategy strategy) {
this.mStrategy = strategy;
}
public void setStrategy(Strategy strategy) {
this.mStrategy = strategy;
}
public void execute() {
mStrategy.execute();
}
}
测试
Context context = new Context(new ConcreteStrategyA());
context.execute();
context.setStrategy(new ConcreteStrategyB());
context.execute();
context.setStrategy(() -> System.out.println("executing lambda expression"));
context.execute();
executing io.binac.designpattern.strategy.ConcreteStrategyA@6325a3ee
executing io.binac.designpattern.strategy.ConcreteStrategyB@1d16f93d
executing lambda expression