责任链模式,请参阅:https://www.jianshu.com/p/75946acd80e3
文章中对责任链模式描述相当清楚,也讲述了几种实现方式,使用中当然选择最优的,也就是作者最后提出的优化方案,即参考OkHttp 里面的 Interceptor 实现方案。
此种方式写一个小Demo是没问题的,确实也写了,上代码前先简单说明,Demo逻辑很简单,根据枚举类型StrategyType决定向哪个方向走,原本用if...else或switch就能实现的功能,使用了责任链来模拟处理,几行的代码变成了几十上百行……
- 基类(BasePayload、接口与基类)
@Getter
@Builder
public class BasePayload {
private StrategyType type;
private Object data;
}
public interface IStrategy {
/**
* 处理方法
*
* @param payload
* @param strategy
*/
void handle(BasePayload payload, IStrategy strategy);
}
@Slf4j
public class BaseStrategy implements IStrategy {
/**
* 策略链
*/
private final List<IStrategy> strategies = Lists.newArrayList();
/**
* 计数器
*/
private AtomicInteger index = new AtomicInteger(0);
/**
* 注册策略
*
* @param strategy
*/
public BaseStrategy register(IStrategy strategy) {
strategies.add(strategy);
return this;
}
@Override
public void handle(BasePayload payload, IStrategy strategy) {
if (index.intValue() >= strategies.size()) {
log.warn("无匹配的处理策略,请检查!");
return;
}
// 获取策略后索引++
IStrategy currStrategy = strategies.get(index.getAndIncrement());
// 策略处理
currStrategy.handle(payload, this);
}
/**
* @ClassName BaseStrategy
* @Description 支持策略类型
* @Author frinder
* @Date 2019/9/5 11:53
* @Verion 1.0
*/
public enum StrategyType {
EAST,
SOUTH,
WEST,
NORTH
}
}
- 策略类
public class EastStrategy implements IStrategy {
@Override
public void handle(BasePayload payload, IStrategy strategy) {
if (EAST.equals(payload.getType())) {
System.out.println("向【北】走10步!");
} else {
// 无法处理,交给下一个处理
strategy.handle(payload, strategy);
}
}
}
public class NorthStrategy implements IStrategy {
@Override
public void handle(BasePayload payload, IStrategy strategy) {
if (NORTH.equals(payload.getType())) {
System.out.println("向【北】走30步!");
} else {
// 无法处理,交给下一个处理
strategy.handle(payload, strategy);
}
}
}
public class SouthStrategy implements IStrategy {
@Override
public void handle(BasePayload payload, IStrategy strategy) {
if (SOUTH.equals(payload.getType())) {
System.out.println("向【南】走5步!");
} else {
// 无法处理,交给下一个处理
strategy.handle(payload, strategy);
}
}
}
public class WestStrategy implements IStrategy {
@Override
public void handle(BasePayload payload, IStrategy strategy) {
if (WEST.equals(payload.getType())) {
System.out.println("向【西】走20步!");
} else {
// 无法处理,交给下一个处理
strategy.handle(payload, strategy);
}
}
}
- 测试方法
public class App {
public static void main(String[] args) {
BaseStrategy strategy = new BaseStrategy();
strategy.register(new EastStrategy())
.register(new SouthStrategy())
.register(new WestStrategy())
.register(new NorthStrategy());
strategy.handle(BasePayload.builder()
.type(SOUTH)
.build(),
strategy
);
}
}
运行结果:
向【南】走5步!
- 再次测试
public class App {
public static void main(String[] args) {
BaseStrategy strategy = new BaseStrategy();
strategy.register(new EastStrategy())
.register(new SouthStrategy())
.register(new WestStrategy())
.register(new NorthStrategy());
strategy.handle(BasePayload.builder()
.type(SOUTH)
.build(),
strategy
);
strategy.handle(BasePayload.builder()
.type(EAST)
.build(),
strategy
);
}
}
运行结果
向【南】走5步!
15:40:03.893 [main] WARN com.kiktech.herd.strategy.base.BaseStrategy - EAST 无匹配的处理策略,请检查!
于是,问题出来了
15:40:03.893 [main] WARN com.kiktech.herd.strategy.base.BaseStrategy - EAST 无匹配的处理策略,请检查!
SOUTH正常处理了,EAST却处理不了,但确实有这个策略的,原因呢?对,计数器。
解决方案:
方案一:每次使用策略时,重新实例化;
方案二:每次调用都初始化计数器,保证不遗漏策略;
当然选择方案二了,每次实例化是个比较消耗资源的操作,特别是策略中有连接数据库啊、TCP连接啊之类的,就更加消耗资源了……
责任链不能滥用,否则会出现几行代码就能干的事变成了几十上百行,事倍功半就事与愿违了!!!
--end