【java】实际项目中责任链模式的应用

实际项目中责任链模式的应用

最近着手进行项目重构,项目中原有责任链的的代码,每次添加新的处理流程,需要重新组装,不方便业务的快速扩展。所以进行了优化处理,别的也不多说,代码如下:

1、首先定义执行链入口

public abstract class AbstractChainHandler {
          
   

    private AbstractChainHandler nextHandler;

    public void setNextHandler(AbstractChainHandler nextHandler) {
          
   
        this.nextHandler = nextHandler;
    }

    protected AbstractChainHandler getNextHandler() {
          
   
        return nextHandler;
    }

    public abstract String handleRequest(ChainRequest request);
}

2、定义实际业务执行链

/**
 * 支付业务
 */
public abstract class AbstractPaymentHandler extends AbstractChainHandler {
          
   
}
@Component
public class AlipayHandler extends AbstractPaymentHandler{
          
   
    @Override
    public String handleRequest(ChainRequest request) {
          
   
        if(request.getType() == 2) {
          
   
            return "alipay";
        } 
        Assert.notNull(getNextHandler(), "暂不支持该操作");
        return getNextHandler().handleRequest(request);
    }
}
@Component
public class WechatPayHandler extends AbstractPaymentHandler{
          
   
    @Override
    public String handleRequest(ChainRequest request) {
          
   
        if(request.getType() == 1) {
          
   
            return "wechat pay";
        } 
        Assert.notNull(getNextHandler(), "暂不支持该操作");
        return getNextHandler().handleRequest(request);
    }
}

2、组装执行链

@Component
@Slf4j
public class ChainContextHandler implements ApplicationContextAware {
          
   

    private static ApplicationContext applicationContext;

    private static Map<String, AbstractChainHandler> handlerMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          
   
        ChainContextHandler.applicationContext = applicationContext;
    }

    private <T extends AbstractChainHandler> AbstractChainHandler getHandler(Class<T> handlerClass) {
          
   
        AbstractChainHandler handler = handlerMap.get(handlerClass.getName());
        if(handler != null){
          
   
            return handler;
        }
        Map<String, T> beans = applicationContext.getBeansOfType(handlerClass);
        AbstractChainHandler chainHandler = null;
        AbstractChainHandler newHandler = null;
        for (String key : beans.keySet()) {
          
   
            if(chainHandler == null){
          
   
                chainHandler = beans.get(key);
                newHandler = chainHandler;
            } else {
          
   
                AbstractChainHandler next = beans.get(key);
                newHandler.setNextHandler(next);
                newHandler = next;
            }
        }
        handlerMap.put(handlerClass.getName(), chainHandler);
        return chainHandler;
    }

    public <T extends AbstractChainHandler> String handleRequest(ChainRequest request, Class<T> handlerClass){
          
   
        return getHandler(handlerClass).handleRequest(request);
    }

}

3、业务代码调用

@RestController 
public class ChainController {
          
   

    @Autowired
    private ChainContextHandler chainContextHandler;

    @PostMapping("pay")
    public String request(ChainRequest request){
          
   
        return chainContextHandler.handleRequest(request, AbstractPaymentHandler.class);

    }
}

4、请求参数

@Getter
@Setter
public class ChainRequest {
          
   
    
    private int type;
    
}

5、测试结果

由于有全局异常捕获,所以返回的error,这里不附捕获的代码

经验分享 程序员 微信小程序 职场和发展