设计模式3-行为模式-责任链模式

news/2024/5/19 12:15:26 标签: 设计模式, 责任链模式, java

文章目录

  • 一、责任链如何设计
    • 1、定义责任链处理对象
      • 1.1、抽象类
      • 1.2、责任链处理对象
    • 2、将“每个链”组合起来,定一个枚举
      • 2.1、枚举对象
      • 2.2、责任链对象
    • 3、调用时,如何获取责任链对象
      • 3.1、获取责任链对象方法
      • 3.2、责任链工厂方法--将这些独立的“链”链接起来,构成责任链,并返回第一个责任链接
    • 4、调用

责任链模式是一种行为设计模式, 允许你将请求沿着处理者链进行发送。收到请求后, 每个处理者均可对请求进行处理, 或将其传递给链上的下个处理者。
在这里插入图片描述

当一个大的流程有很多步骤时,可以将些步骤链接起来 形成一个责任链,如果符合条件那么调用下一个链,如果不符合条件 那么跳过所有链。

一、责任链如何设计

1、定义责任链处理对象

每个链需要处理的逻辑,有一个统一的方法,这样便于统一调用,可以继承一个公共的抽象类

1.1、抽象类

java">public abstract  class AbstractHandler {

    /**
     * 下一关用当前抽象类来接收
     */
    protected AbstractHandler next;

    public void setNext(AbstractHandler next) {
        this.next = next;
    }

    public abstract int handler();
}

1.2、责任链处理对象

java">public class FirstPassHandler extends AbstractHandler {

    private int play(){
        return 80;
    }

    @Override
    public int handler(){
        System.out.println("第一关-->FirstPassHandler");
        int score = play();
        if(score >= 80){
            //分数>=80 并且存在下一关才进入下一关
            if(this.next != null){
                return this.next.handler();
            }
        }
        return score;
    }
}
java">public class SecondPassHandler extends AbstractHandler  {


    private int play(){
        return 90;
    }

    public int handler(){
        System.out.println("第二关-->SecondPassHandler");

        int score = play();
        if(score >= 90){
            //分数>=90 并且存在下一关才进入下一关
            if(this.next != null){
                return this.next.handler();
            }
        }

        return score;
    }
}

2、将“每个链”组合起来,定一个枚举

2.1、枚举对象

java">public enum  GatewayEnum {
    // handlerId, 拦截者名称,全限定类名,preHandlerId,nextHandlerId
    FIRST_HANDLER(new GatewayEntity(1, "api接口限流", "gateway.handler.FirstPassHandler", null, 2)),
    SECOND_HANDLER(new GatewayEntity(2, "黑名单拦截", "gateway.handler.SecondPassHandler", 1, null)),
    /*如果要 新增第三个责任链,则 再定一个枚举值,将 上一个枚举的 nextId 定义成 第新增的Id*/
    /*SESSION_HANDLER(new GatewayEntity(3, "用户会话拦截", "cn.dgut.design.chain_of_responsibility.GateWay.impl.SessionGatewayHandler", 2, null)),*/
    ;

    GatewayEntity gatewayEntity;

    public GatewayEntity getGatewayEntity() {
        return gatewayEntity;
    }

    GatewayEnum(GatewayEntity gatewayEntity) {
        this.gatewayEntity = gatewayEntity;
    }
}

2.2、责任链对象

java">@Setter
@Getter
public class GatewayEntity {

    private String name;

    private String conference;

    private Integer handlerId;

    private Integer preHandlerId;

    private Integer nextHandlerId;

    public GatewayEntity(Integer handlerId,String name,String conference,Integer preId,Integer nextId){

        this.handlerId = handlerId;
        this.name = name;
        this.conference = conference;
        this.preHandlerId = preId;
        this.nextHandlerId = nextId;

    }
}

3、调用时,如何获取责任链对象

如果要调用这个责任链,那么肯定要将这些独立的“链” 链接起来,并获取第一个责任链对象,然后就可以调用了

3.1、获取责任链对象方法

java">public interface GatewayDao {

    /**
     * 根据 handlerId 获取配置项
     * @param handlerId
     * @return
     */
    GatewayEntity getGatewayEntity(Integer handlerId);

    /**
     * 获取第一个处理者
     * @return
     */
    GatewayEntity getFirstGatewayEntity();
}
java">public class GatewayImpl implements GatewayDao {

    /**
     * 初始化,将枚举中配置的handler初始化到map中,方便获取
     */
    private static Map<Integer, GatewayEntity> gatewayEntityMap = new HashMap<>();

    static {
        GatewayEnum[] values = GatewayEnum.values();
        for (GatewayEnum value : values) {
            GatewayEntity gatewayEntity = value.getGatewayEntity();
            gatewayEntityMap.put(gatewayEntity.getHandlerId(), gatewayEntity);
        }
    }


    @Override
    public GatewayEntity getGatewayEntity(Integer handlerId) {
        return gatewayEntityMap.get(handlerId);
    }

    @Override
    public GatewayEntity getFirstGatewayEntity() {
        for (Map.Entry<Integer, GatewayEntity> entry : gatewayEntityMap.entrySet()) {
            GatewayEntity value = entry.getValue();
            //  没有上一个handler的就是第一个
            if (value.getPreHandlerId() == null) {
                return value;
            }
        }
        return null;    
    }
}

3.2、责任链工厂方法–将这些独立的“链”链接起来,构成责任链,并返回第一个责任链接

java">public class GatewayHandlerEnumFactory {

    private static GatewayDao gatewayDao = new GatewayImpl();
    // 提供静态方法,获取第一个handler

    /**
     * 设置 责任链 并返回第第一天 责任链
     * @return
     */
    public static AbstractHandler getFirstGatewayHandler() {

        GatewayEntity firstGatewayEntity = gatewayDao.getFirstGatewayEntity();
        AbstractHandler firstGatewayHandler = newGatewayHandler(firstGatewayEntity);
        if (firstGatewayHandler == null) {
            return null;
        }

        GatewayEntity tempGatewayEntity = firstGatewayEntity;
        Integer nextHandlerId = null;
        AbstractHandler tempGatewayHandler = firstGatewayHandler;
        // 迭代遍历所有handler,以及将它们链接起来
        while ((nextHandlerId = tempGatewayEntity.getNextHandlerId()) != null) {
            GatewayEntity gatewayEntity = gatewayDao.getGatewayEntity(nextHandlerId);
            AbstractHandler gatewayHandler = newGatewayHandler(gatewayEntity);
            tempGatewayHandler.setNext(gatewayHandler);
            tempGatewayHandler = gatewayHandler;
            tempGatewayEntity = gatewayEntity;
        }
        // 返回第一个handler
        return firstGatewayHandler;
    }

    /**
     * 反射实体化具体的处理者
     * @param firstGatewayEntity
     * @return
     */
    private static AbstractHandler newGatewayHandler(GatewayEntity firstGatewayEntity) {
        // 获取全限定类名
        String className = firstGatewayEntity.getConference();
        try {
            // 根据全限定类名,加载并初始化该类,即会初始化该类的静态段
            Class<?> clazz = Class.forName(className);
            return (AbstractHandler) clazz.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}

4、调用

java">public class GetewayClient {

    public static void main(String[] args) {
        AbstractHandler firstGetewayHandler = GatewayHandlerEnumFactory.getFirstGatewayHandler();
        firstGetewayHandler.handler();
    }
}

http://www.niftyadmin.cn/n/5390979.html

相关文章

数字电路 第二章—第一节(门电路—概述)

一、门电路的概念 实现基本和常用逻辑运算的电子电路称为逻辑门电路&#xff0c;简称门电路。例如&#xff0c;实现与运算的称为与门&#xff0c;实现或运算的称为或门&#xff0c;实现非运算的称为非门&#xff0c;也称为反相器&#xff1b;类似地&#xff0c;实现与非、或非、…

【尚硅谷】MybatisPlus 学习笔记(下)

目录 六、插件 6.1、分页插件 6.1.1、添加配置类 6.1.2、测试 6.2、xml自定义分页 6.2.1、UserMapper中定义接口方法 6.2.2、UserMapper.xml中编写SQL 6.2.3、测试 6.3、乐观锁 6.3.1、场景 6.3.2、乐观锁与悲观锁 6.3.3、模拟修改冲突 数据库中增加商品表 添加数…

【开源】JAVA+Vue.js实现校园电商物流云平台

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 商品数据模块2.3 快递公司模块2.4 物流订单模块 三、系统设计3.1 用例设计3.2 数据库设计3.2.1 商品表3.2.2 快递公司表3.2.3 物流订单表 四、系统展示五、核心代码5.1 查询商品5.2 查询快递公司5.3 查…

牛客网 HJ10 字符个数统计

思路&#xff1a; 我们创建两个数组&#xff0c;一个数组接受输入的字符&#xff0c;另一个数组用来统计字符种数 同时将该字符作为下标传给另一个数组&#xff0c;如果另一个数组的这个下标对应的值为0&#xff0c;说明该字符没有被统计过&#xff0c;计数器加1&#xff0c;…

Windows+Yolo3-darknet训练自己数据集并测试

WindowsYolo3-darknet训练自己的数据集并测试 一、首要条件 Windows 7下配置好VS2015OPENCV3.4.2YOLO3CUDA10.0CUDNN7.5生成darknet.exe。具体配置可参考我的博客&#xff1a;https://blog.csdn.net/wszswllnzn_/article/details/100760477 二.制作数据集 1、方法1 使用软件la…

第九届大数据与计算国际会议 (ICBDC 2024) 即将召开!

2024年第九届大数据与计算国际会议&#xff08;ICBDC 2024&#xff09;将于2024年5月24至26日在泰国曼谷举行。本次会议由朱拉隆功大学工程学院工业工程系主办。ICBDC 2024的宗旨是展示大数据和计算主题相关科学家的最新研究和成果&#xff0c;为来自不同地区的专家代表们提供一…

C++惯用法之CRTP(奇异递归模板模式)

相关系列文章 C惯用法之Pimpl C之数据转换(全) 目录 1.介绍 2.CRTP的使用场景 2.1.实现静态多态 2.2.代码复用和扩展性 3.总结 1.介绍 CRTP的全称是Curiously Recurring Template Pattern&#xff0c;即奇异递归模板模式&#xff0c;简称CRTP。CRTP是一种特殊的模板技术和…

车载电子电器架构 —— 电气架构释放检查

车载电子电器架构 —— 电气架构释放检查 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明…