当前位置: 首页 > article >正文

行为型模式 - 职责链模式 (Chain of Responsibility Pattern)

行为型模式 - 职责链模式 (Chain of Responsibility Pattern)

职责链模式是一种行为设计模式,它允许你将请求沿着处理者链进行传递,直到有一个处理者能够处理该请求为止。以下是几个职责链模式的经典案例。


在企业中,员工请假需要不同级别的领导进行审批,根据请假天数的不同,审批流程也有所不同。例如,请假天数小于 3 天,只需部门经理审批;请假天数在 3 - 7 天之间,需要部门经理和总监审批;请假天数大于 7 天,需要部门经理、总监和总经理审批。

// 抽象处理者类
abstract class Approver {
    protected Approver successor;

    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    public abstract void processRequest(int days);
}

// 部门经理类
class DepartmentManager extends Approver {
    @Override
    public void processRequest(int days) {
        if (days < 3) {
            System.out.println("部门经理批准了 " + days + " 天的请假申请");
        } else if (successor != null) {
            successor.processRequest(days);
        }
    }
}

// 总监类
class Director extends Approver {
    @Override
    public void processRequest(int days) {
        if (days >= 3 && days < 7) {
            System.out.println("总监批准了 " + days + " 天的请假申请");
        } else if (successor != null) {
            successor.processRequest(days);
        }
    }
}

// 总经理类
class GeneralManager extends Approver {
    @Override
    public void processRequest(int days) {
        if (days >= 7) {
            System.out.println("总经理批准了 " + days + " 天的请假申请");
        }
    }
}

// 客户端代码
public class LeaveApprovalSystem {
    public static void main(String[] args) {
        Approver departmentManager = new DepartmentManager();
        Approver director = new Director();
        Approver generalManager = new GeneralManager();

        departmentManager.setSuccessor(director);
        director.setSuccessor(generalManager);

        departmentManager.processRequest(2);
        departmentManager.processRequest(5);
        departmentManager.processRequest(10);
    }
}

再举个例子, 游戏中判断伤害, 分为物理伤害魔法伤害, 可以链式处理

// 抽象处理者类
abstract class AttackHandler {
    protected AttackHandler nextHandler;

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

    public abstract void handleAttack(Attack attack);
}

// 攻击类
class Attack {
    private String type;

    public Attack(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

// 普通攻击处理者类
class NormalAttackHandler extends AttackHandler {
    @Override
    public void handleAttack(Attack attack) {
        if ("normal".equals(attack.getType())) {
            System.out.println("处理普通攻击");
        } else if (nextHandler != null) {
            nextHandler.handleAttack(attack);
        }
    }
}

// 魔法攻击处理者类
class MagicAttackHandler extends AttackHandler {
    @Override
    public void handleAttack(Attack attack) {
        if ("magic".equals(attack.getType())) {
            System.out.println("处理魔法攻击");
        } else if (nextHandler != null) {
            nextHandler.handleAttack(attack);
        }
    }
}

// 客户端代码
public class GameAttackHandling {
    public static void main(String[] args) {
        AttackHandler normalAttackHandler = new NormalAttackHandler();
        AttackHandler magicAttackHandler = new MagicAttackHandler();

        normalAttackHandler.setNextHandler(magicAttackHandler);

        Attack normalAttack = new Attack("normal");
        Attack magicAttack = new Attack("magic");

        normalAttackHandler.handleAttack(normalAttack);
        normalAttackHandler.handleAttack(magicAttack);
    }
}

http://www.kler.cn/a/565530.html

相关文章:

  • 我与Swagger-UI的量子纠缠:SpringBoot3.x中的薛定谔404事件——解决`springdoc-openapi:2.8.5`UI界面显示问题
  • 【Python pro】函数
  • redis密码设置
  • 如何实现某短视频平台批量作品ID的作品详情采集
  • PySide(PyQT)重新定义contextMenuEvent()实现鼠标右键弹出菜单
  • 销售易NeoCRM与八骏科技CRM:全方位深度对比
  • 浅聊RocketMQ 分布式事务解决方案原理
  • Spock框架:让单元测试更优雅的高效武器
  • QT 读取sqlite3数据库中文乱码
  • 字段对比清洗
  • [MRCTF2020]Ezpop
  • 搜索赋能:大型语言模型的知识增强与智能提升
  • Deepseek开源周第一天:FlashMLA来袭
  • 自定义注解 + AOP + Redisson:优雅实现分布式锁(增强版)
  • Go 语言内存池 (`sync.Pool`) 深度解析
  • 腿足机器人之十三-强化学习PPO算法
  • AI+游戏,正在进行时!
  • svn忽略文件
  • Unity XR-XR Interaction Toolkit开发使用方法(八)组件介绍(XR Socket Interactor)
  • 每天一个Flutter开发小项目 (6) : 表单与验证的专业实践 - 构建预约应用