责任链模式在Java中的实现与应用
责任链模式简介
责任链模式(Chain of Responsibility Pattern)是一种行为型设计模式,它允许多个对象有机会处理请求,从而避免请求的发送者和接收者之间的耦合。通过将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
一个典型的应用场景是权限审批系统。例如,在企业采购流程中,不同级别的管理人员拥有不同的审批额度。小额支出由基层员工处理,较大金额则需逐级上报至更高管理层。这种层级式决策结构非常适合使用责任链模式来建模。
核心角色定义
该模式包含两个主要组成部分:抽象处理者(Handler)和具体处理者(Concrete Handler)。抽象处理者定义了处理请求的接口,并持有对下一个处理者的引用;具体处理者实现实际的业务逻辑并决定是否继续传递请求。
定义抽象处理者
/**
* 抽象价格审批处理器
*/
public abstract class ApprovalHandler {
protected ApprovalHandler nextApprover;
public void setNext(ApprovalHandler handler) {
this.nextApprover = handler;
}
public abstract void handleRequest(double discountRate);
}
实现具体处理者
每个具体的审批角色继承自 ApprovalHandler,根据自身权限判断是否可以处理当前请求。
/**
* 销售代表:可批准不超过5%的折扣
*/
public class SalesRepresentative extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate <= 0.05) {
System.out.println("销售代表已批准 " + formatPercent(discountRate));
} else if (nextApprover != null) {
nextApprover.handleRequest(discountRate);
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
/**
* 区域经理:可处理最高30%的折扣申请
*/
public class RegionalManager extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate <= 0.30) {
System.out.println("区域经理已批准 " + formatPercent(discountRate));
} else if (nextApprover != null) {
nextApprover.handleRequest(discountRate);
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
/**
* 总监级别:支持最高40%的优惠幅度
*/
public class Director extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate <= 0.40) {
System.out.println("总监已批准 " + formatPercent(discountRate));
} else if (nextApprover != null) {
nextApprover.handleRequest(discountRate);
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
/**
* 副总裁:最大权限为50%
*/
public class VicePresident extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate <= 0.50) {
System.out.println("副总裁已批准 " + formatPercent(discountRate));
} else if (nextApprover != null) {
nextApprover.handleRequest(discountRate);
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
/**
* 公司CEO:最终决策者,最多允许55%折扣
*/
public class ChiefExecutiveOfficer extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate <= 0.55) {
System.out.println("CEO已批准 " + formatPercent(discountRate));
} else {
System.out.println("超出最高权限,无法批准 " + formatPercent(discountRate));
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
构建处理链条
为了简化对象创建过程,我们引入工厂类来组装完整的责任链。
public class ApprovalChainFactory {
public static ApprovalHandler buildChain() {
ApprovalHandler sales = new SalesRepresentative();
ApprovalHandler teamLead = new TeamLeader(); // 新增小组长角色
ApprovalHandler regionalMgr = new RegionalManager();
ApprovalHandler director = new Director();
ApprovalHandler vp = new VicePresident();
ApprovalHandler ceo = new ChiefExecutiveOfficer();
// 链接各节点形成处理链
sales.setNext(teamLead);
teamLead.setNext(regionalMgr);
regionalMgr.setNext(director);
director.setNext(vp);
vp.setNext(ceo);
return sales; // 返回起始节点
}
}
新增团队领导角色
随着组织架构调整,可在链中插入新的处理节点以适应变化。
/**
* 团队负责人:具备15%以内的审批权
*/
public class TeamLeader extends ApprovalHandler {
@Override
public void handleRequest(double discountRate) {
if (discountRate < 0.15) {
System.out.println("团队负责人已批准 " + formatPercent(discountRate));
} else if (nextApprover != null) {
nextApprover.handleRequest(discountRate);
}
}
private String formatPercent(double rate) {
return String.format("%.1f%%", rate * 100);
}
}
客户端测试代码
模拟客户发起多个折扣申请,观察请求如何在链上传递。
public class DiscountClient {
public static void main(String[] args) {
ApprovalHandler chainHead = ApprovalChainFactory.buildChain();
java.util.Random random = new java.util.Random();
for (int i = 1; i <= 10; i++) {
double request = random.nextDouble();
System.out.print("请求 #" + i + ": ");
chainHead.handleRequest(request);
}
}
}
运行结果将显示每个请求被哪一级别处理,或因超出权限而被拒绝。这种结构使得系统易于扩展——添加新职位只需实现新类并将其接入链中,无需修改已有逻辑。