责任链模式顾名思义就是。执行一串业务处理,像链子一样,从上向下,一个节点一个节点有序执行,中间任何一个节点挂啦,链子就断了,不会继续向下执行
实现思路就是把一个个处理器类放到一个数组list中,遍历数组,从第一个到末尾一个个执行 handle 方法,数组list就是一个链子,一个个对象的处理方法就是责任,合起来就是责任链咯!
import org.springframework.lang.Nullable;/*** 描述: 业务处理顶级拦截器接口 </br>* 时间: 2021-03-02 14:46 </br>* 作者:IT学习道场*/public interface HandleIntercept {/*** 业务顶级接口处理接口* @param model 泛型参数* @return 成功的对象返回,可以是boolean,string,model*/Object handle(@Nullable Object model);}
第一个业务处理拦截器
import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/*** 描述: 第一个业务处理拦截器 </br>* 时间: 2021-03-02 14:57 </br>* 作者:IT学习道场*/@Order(2)@Componentpublic class FirstHandleIntercept implements HandleIntercept {@Overridepublic Object handle(Object model) {System.out.println("========执行 firstHandleIntercept 开始业务处理 =======");return true;}}
第二业务处理器
import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/*** 描述: 第二业务处理器 </br>* 时间: 2021-03-02 15:02 </br>* 作者:IT学习道场*/@Order(1)@Componentpublic class SecondHandleIntercept implements HandleIntercept {@Overridepublic Object handle(Object model) {System.out.println("========执行 SecondHandleIntercept =======");return true;}}
第三业务处理器
import org.springframework.stereotype.Component;/*** 描述: 第二业务处理器 </br>* 时间: 2021-03-02 15:02 </br>* 作者:IT学习道场*///@Order(1)@Componentpublic class ThirdHandleIntercept implements HandleIntercept {@Overridepublic Object handle(Object model) {System.out.println("========执行 ThirdHandleIntercept =======");return true;}}
业务处理器门面入口
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import org.springframework.util.ObjectUtils;import javax.annotation.PostConstruct;import java.util.*;import java.util.concurrent.ConcurrentHashMap;/*** 描述: 业务处理器门面入口 </br>* 时间: 2021-03-02 15:05 </br>* 作者:IT学习道场*/@Componentpublic class HandleInterceptEntrance {@Autowiredprivate Map<String, HandleIntercept> HandleInterceptBeanMap = new ConcurrentHashMap<>();//业务链式处理器listprivate static List<HandleIntercept> HandleInterceptList = new ArrayList<>();/*** 链式处理入口,类似门面*/public void handleEntrance() {if (CollectionUtils.isEmpty(HandleInterceptList)){getHandleInterceptList ();}for (HandleIntercept handleIntercept : HandleInterceptList) {//要注入参数,可以在此处理handleIntercept.handle(null);}}/*** 处理拦截器拍寻,解决order排序不生效问题* 并把HandleIntercept注入到HandleInterceptList中*/@PostConstructprivate synchronized void getHandleInterceptList () {//存贮不带order注解的拦截器容器listList<HandleIntercept> noOrderHandleInterceptList = new ArrayList<>();//存贮带order注解的拦截器容器listList<HandleIntercept> orderHandleInterceptList = new ArrayList<>();//处理HandleIntercept的springioc容器中的HandleIntercept,处理到//noOrderHandleInterceptList和orderHandleInterceptListfor (String beanName : HandleInterceptBeanMap.keySet()) {HandleIntercept handleIntercept = HandleInterceptBeanMap.get(beanName);//获取order注解Order order = handleIntercept.getClass().getAnnotation(Order.class);if (ObjectUtils.isEmpty(order)){noOrderHandleInterceptList.add(handleIntercept);}else {orderHandleInterceptList.add(handleIntercept);}}//对orderHandleInterceptList进行oder注解的value值升序排列if (!CollectionUtils.isEmpty(orderHandleInterceptList)){Collections.sort(orderHandleInterceptList, new Comparator<HandleIntercept>() {@Overridepublic int compare(HandleIntercept h1, HandleIntercept h2) {//升序Integer value1 = h1.getClass().getAnnotation(Order.class).value();Integer value2 = h2.getClass().getAnnotation(Order.class).value();return value1.compareTo(value2);}});}//优先orderHandleInterceptList注入HandleInterceptListHandleInterceptList.addAll(orderHandleInterceptList);HandleInterceptList.addAll(noOrderHandleInterceptList);}}
使用示例demo
import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@Slf4j@RestController@RequestMapping("/test")public class HandleController {@Autowiredprivate HandleInterceptEntrance handleEntrance;@GetMapping("/handleEntrance")public void test() {handleEntrance.handleEntrance();}}
责任链模式顾名思义就是。执行一串业务处理,像链子一样,从上向下,一个节点一个节点有序执行,中间任何一个节点挂啦,链子就断了,不会继续向下执行
实现思路就是把一个个处理器类放到一个数组list中,遍历数组,从第一个到末尾一个个执行 handle 方法,数组list就是一个链子,一个个对象的处理方法就是责任,合起来就是责任链咯!
其实这里面包含了,工厂模式,单例模式。门面模式和责任链综合使用,很多情况都是多种模式配合使用
看明白后,要自己动手玩玩吧,自己写,不要抄,实在搞不定,再看看,不要copy,使用场景需要一连串的不同的业务处理的操作场景,可以考虑责任链,经典场景:ip很名单处理 ->登录处理 -> 权限处理等类似不同处理维度的连续不间断处理,考虑责任链
文章转载自IT学习道场,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




