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

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

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


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

java 复制代码
// 抽象处理者类
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);
    }
}

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

java 复制代码
// 抽象处理者类
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);
    }
}
相关推荐
七月丶13 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞13 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼14 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟1 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder1 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室2 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo6 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4966 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃6 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式