行为型模式 - 职责链模式 (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);
    }
}
相关推荐
烛阴9 小时前
【TS 设计模式完全指南】从“入门”到“劝退”,彻底搞懂单例模式
javascript·设计模式·typescript
Meteors.11 小时前
23种设计模式——原型模式 (Prototype Pattern)详解
设计模式·原型模式
摘星编程1 天前
CodeBuddy 辅助重构:去掉 800 行 if-else 的状态机改造
设计模式·代码重构·技术债务·codebuddy·状态机模式
Meteors.1 天前
23种设计模式——策略模式 (Strategy Pattern)详解
设计模式·策略模式
念念不忘 必有回响1 天前
js设计模式-装饰器模式
javascript·设计模式·装饰器模式
Meteors.1 天前
23种设计模式——代理模式(Proxy Pattern)详解
设计模式·代理模式
晨星05272 天前
软件设计模式之单例模式
单例模式·设计模式
Meteors.2 天前
23种设计模式——装饰器模式(Decorator Pattern)详解
java·设计模式·装饰器模式
谢栋_2 天前
设计模式从入门到精通之(六)策略模式
设计模式·bash·策略模式