行为型模式 - 职责链模式 (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);
    }
}
相关推荐
却尘3 小时前
跨域资源共享(CORS)完全指南:从同源策略到解决方案 (1)
前端·设计模式
coderzpw6 小时前
设计模式中的“万能转换器”——适配器模式
设计模式·适配器模式
三金C_C13 小时前
单例模式解析
单例模式·设计模式·线程锁
ShareBeHappy_Qin15 小时前
设计模式——设计模式理念
java·设计模式
木子庆五17 小时前
Android设计模式之代理模式
android·设计模式·代理模式
前端_ID林19 小时前
前端必须知道的设计模式
设计模式
麦客奥德彪21 小时前
设计模式分类与应用指南
设计模式
小宋要上岸21 小时前
设计模式-单例模式
单例模式·设计模式
程序员JerrySUN1 天前
设计模式 Day 1:单例模式(Singleton Pattern)详解
单例模式·设计模式
古力德1 天前
代码重构之[过长参数列表]
设计模式·代码规范