行为型模式 - 职责链模式 (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);
    }
}
相关推荐
小王子10241 小时前
设计模式Python版 观察者模式(上)
python·观察者模式·设计模式
Duramentee1 小时前
C++ 设计模式 十九:观察者模式 (读书 现代c++设计模式)
c++·观察者模式·设计模式
工一木子3 小时前
【HeadFirst系列之HeadFirst设计模式】第14天之复合模式:设计模式的终极组合拳!
设计模式·复合模式
千里码!6 小时前
java23种设计模式-模板方法模式
java·设计模式·模板方法模式
攻城狮7号6 小时前
【第八节】C++设计模式(结构型模式)-Decorator(装饰器)模式
c++·设计模式·装饰器模式
北城望戈8 小时前
工作中遇到的设计模式整理
java·设计模式
LuckyLay8 小时前
Golang学习笔记_40——模版方法模式
笔记·学习·设计模式·golang·模板方法模式
Forget the Dream10 小时前
设计模式之责任链模式
java·c++·设计模式·责任链模式
Duramentee11 小时前
C++ 设计模式 十二:责任链模式 (读书 现代c++设计模式)
c++·设计模式·责任链模式