责任链设计模式

 流水线的干活 !!!!!!

责任链设计模式_第1张图片

 责任链设计模式_第2张图片

package com.dj.springtest.demo;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 15:37
 * Description: 责任链模式测试
 */
public abstract class GatewayHandler {

    /**
     * 下一个步骤的执行处理器
     */
    protected GatewayHandler nextHandler;

    /**
     * 设置下一个步骤的执行处理器
     */
    public void setNextHandler(GatewayHandler handler){
        this.nextHandler = handler;
    }

    /**
     * 用于业务拓展的抽象方法
     */
    public abstract void execute();
}

 责任链设计模式_第3张图片

责任链设计模式_第4张图片

 责任链设计模式_第5张图片

 后续补充:  变更点节点之间串联策略改成配置的

package com.dj.springtest.constant;

import com.dj.springtest.model.entity.GatewayStrategyEntity;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 22:51
 * Description: 网关策略配置,还可以做成配置文件形式
 * 如: api_flow_handler=1,null,2,com.dj.springtest.component.FlowGatewayHandler,Api接口限流
 * 使用 @Value 拿到配置value.split(),根据下标拿到对映的值
 */
public enum GatewayStrategyEnum {

    //枚举配置了节点之间的执行顺序
    API_FLOW_HANDLER(new GatewayStrategyEntity(1, null, 2, "com.dj.springtest.component.FlowGatewayHandler", "Api接口限流")),
    BLACK_LIST_HANDLER(new GatewayStrategyEntity(2, 1, 3, "com.dj.springtest.component.BlackListGatewayHandler", "黑名单拦截")),
    USER_SESSION_HANDLER(new GatewayStrategyEntity(3, 2, null, "com.dj.springtest.component.SessionGatewayHandler", "用户会话拦截"));


    GatewayStrategyEntity gatewayEntity;

    GatewayStrategyEnum(GatewayStrategyEntity entity) {
        this.gatewayEntity = entity;
    }

    public GatewayStrategyEntity getGatewayEntity() {
        return gatewayEntity;
    }

    public void setGatewayEntity(GatewayStrategyEntity gatewayEntity) {
        this.gatewayEntity = gatewayEntity;
    }

}
package com.dj.springtest.factory;

import com.dj.springtest.component.abstractClass.GatewayHandler;
import com.dj.springtest.model.entity.GatewayStrategyEntity;
import com.dj.springtest.service.GatewayService;
import com.dj.springtest.service.impl.GatewayServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 22:25
 * Description: 工厂类
 */
@Slf4j
public class GatewayHandlerFactory {

    private static GatewayService gatewayService = new GatewayServiceImpl();

    /**
     * 配置
     * @return
     */
    public static GatewayHandler getStarterGatewayHandler() {

        //获取首节点网关处理器
        GatewayStrategyEntity firstNodeStrategyEntity = gatewayService.getFirstNodeGatewayStrategy();
        GatewayHandler firstNodeGatewayHandler = createGatewayHandler(firstNodeStrategyEntity);
        if (Objects.isNull(firstNodeGatewayHandler)) {
            return null;
        }

        Integer postHandlerId = null;
        //临时数据,在避免遍历时,覆盖最初的值
        GatewayHandler tempGatewayHandler = firstNodeGatewayHandler;

        //TODO >>>>>>> 核心:串联所有的节点(GatewayHandler) <<<<<<<<
        //如果第一个节点有下一个节点,就根据postHandlerId获取下一个节点策略实体并实例化下一个GatewayHandler
        while ((postHandlerId = firstNodeStrategyEntity.getPostHandlerId()) != null) {
            GatewayStrategyEntity nextStrategyEntity = gatewayService.getGatewayStrategyById(postHandlerId);
            GatewayHandler nextGatewayHandler = createGatewayHandler(nextStrategyEntity);
            if (Objects.nonNull(nextGatewayHandler)) {
                //第一(上)个节点串联下一个节点
                firstNodeGatewayHandler.setNextHandler(nextGatewayHandler);
                //将遍历对象改成下一个节点
                firstNodeStrategyEntity = nextStrategyEntity;
                firstNodeGatewayHandler = nextGatewayHandler;
            }
        }

        return tempGatewayHandler;
    }


    /**
     * 根据全限定类名反射实例化GatewayHandler
     *
     * @param gatewayEntity
     * @return
     */
    private static GatewayHandler createGatewayHandler(GatewayStrategyEntity gatewayEntity) {
        //全限定类名
        String reference = gatewayEntity.getReference();
        try {
            Class clazz = Class.forName(reference);
            return (GatewayHandler) clazz.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            log.error("[GatewayHandlerFactory] [{}]初始化失败!", reference, e);
        }
        return null;
    }

}
package com.dj.springtest.service;

import com.dj.springtest.model.entity.GatewayStrategyEntity;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 23:18
 * Description: No Description
 */
public interface GatewayService {

    /**
     * 根据handlerId获取网关策略
     */
    GatewayStrategyEntity getGatewayStrategyById(Integer handlerId);

    /**
     * 获取首节点(链路第一个节点)的网关策略
     */
    GatewayStrategyEntity getFirstNodeGatewayStrategy();
}
package com.dj.springtest.service.impl;

import com.dj.springtest.constant.GatewayStrategyEnum;
import com.dj.springtest.model.entity.GatewayStrategyEntity;
import com.dj.springtest.service.GatewayService;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 23:27
 * Description: No Description
 */
public class GatewayServiceImpl implements GatewayService {

    private static Map gatewayStrategyMap;

    //初始化加载网关策略配置信息
    static {
        GatewayStrategyEnum[] strategy = GatewayStrategyEnum.values();
        gatewayStrategyMap = Arrays.stream(strategy).collect(Collectors.toMap(k -> k.getGatewayEntity().getHandlerId(), GatewayStrategyEnum::getGatewayEntity));
    }

    @Override
    public GatewayStrategyEntity getGatewayStrategyById(Integer handlerId) {
        return gatewayStrategyMap.get(handlerId);
    }

    @Override
    public GatewayStrategyEntity getFirstNodeGatewayStrategy() {
        for (Map.Entry strategyEntityEntry : gatewayStrategyMap.entrySet()) {
            GatewayStrategyEntity gatewayStrategyEntity = strategyEntityEntry.getValue();
            Integer preHandlerId = gatewayStrategyEntity.getPreHandlerId();
            //当前的GatewayHandler没有指向上一个节点,那它就是第一个
            if(Objects.isNull(preHandlerId)){
                return gatewayStrategyEntity;
            }
        }
        return null;
    }

}
package com.dj.springtest.demo;

import com.dj.springtest.component.abstractClass.GatewayHandler;
import com.dj.springtest.factory.GatewayHandlerFactory;

import java.util.Objects;

/**
 * User: ldj
 * Date: 2023/10/16
 * Time: 0:53
 * Description: No Description
 */
public class TestGatewayHandler2 {

    public static void main(String[] args) {
        GatewayHandler firstNodeGatewayHandler = GatewayHandlerFactory.getStarterGatewayHandler();
        if(Objects.nonNull(firstNodeGatewayHandler)){
            firstNodeGatewayHandler.execute();
        }
    }
}

责任链设计模式_第6张图片

你可能感兴趣的:(设计模式,java,开发语言)