责任链模式应用
1. 责任链模式介绍
责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
2. 应用场景
- 1). 有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定。
- 2). 在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
- 3). 可动态指定一组对象处理请求。
3. 实现(以创建订单为例子)
步骤一
创建一个接口:
package com.example.demo.designmodel.chain3;
import org.springframework.core.Ordered;
/**
* @author: robin
* @date: 2020/8/6 4:44 PM
* @aphorism: "stay hungry, stay foolish"
*/
public interface AbstractOrderHandler extends Ordered {
/**
*
* provide abstract method
*
* @param stage
* @param ride
* @return
*/
Boolean doHandler(int stage, GreenChannelOrder ride);
}
步骤二
创建责任链处理器添加后继处理器:
package com.example.demo.designmodel.chain3;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* Chain of Responsibility Pattern
* Ensure link order
*/
@Component
public class OrderHandlerChain {
private List<AbstractOrderHandler> baseHandlers;
//private Map<String, AbstractOrderHandler> map2 = Maps.newHashMapWithExpectedSize(3);
public OrderHandlerChain(List<AbstractOrderHandler> baseHandlers) {
baseHandlers.sort(Comparator.comparing(AbstractOrderHandler::getOrder));
/* Map<String, AbstractOrderHandler> map = Maps.newHashMapWithExpectedSize(3);
for(AbstractOrderHandler handler:baseHandlers){
map.put(handler.handLerName(),handler);
}
*/
// this.map2=baseHandlers.stream().collect(Collectors.toMap(AbstractOrderHandler::handLerName, a -> a,(k1, k2)->k1));
this.baseHandlers = Collections.unmodifiableList(baseHandlers);
}
/* public AbstractOrderHandler getAbstractHandler(String handlerName){
return map2.get(handlerName);
}*/
public void handle(int stage, GreenChannelOrder ride) {
boolean handle = false;
for (AbstractOrderHandler baseHandler : baseHandlers) {
if (!handle) {
handle = baseHandler.doHandler(stage, ride);
}
}
}
}
步骤三
创建具体的职责处理类
1)预置订单处理器
package com.example.demo.designmodel.chain3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author: robin
* @date: 2020/8/6 4:46 PM
* @aphorism: "stay hungry, stay foolish"
*/
@Slf4j
@Component
public class PreOrderHandler implements AbstractOrderHandler {
@Override
public Boolean doHandler(int stage, GreenChannelOrder ride) {
if (stage <= 1) { //预置订单处理职责
log.info("阶段1");
System.out.println("阶段1");
return false;
}
return false;//继续传递
}
@Override
public int getOrder() {
return 0;
}
}
2)开始订单处理器
package com.example.demo.designmodel.chain3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author: robin
* @date: 2020/8/6 4:46 PM
* @aphorism: "stay hungry, stay foolish"
*/
@Slf4j
@Component
public class StartOrderHandler implements AbstractOrderHandler {
@Override
public Boolean doHandler(int stage, GreenChannelOrder ride) {
if (stage <= 2) {//开始订单处理职责
log.info("阶段2");
return true;
}
return false;
}
@Override
public int getOrder() {
return 1;
}
}
3)结束订单处理器
package com.example.demo.designmodel.chain3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author: robin
* @date: 2020/8/6 4:46 PM
* @aphorism: "stay hungry, stay foolish"
*/
@Slf4j
@Component
public class FinishOrderHandler implements AbstractOrderHandler {
@Override
public Boolean doHandler(int stage, GreenChannelOrder ride) {
if (stage <= 3) {//结束订单处理职责
try {
log.info("阶段3");
} catch (Exception e) {
}
}
return true;
}
@Override
public int getOrder() {
return 2;
}
}