当前位置: 首页 > news >正文

《 Spring Boot实战:优雅构建责任链模式投诉处理业务》

Spring Boot实战:优雅构建责任链模式投诉处理业务

文章目录

  • Spring Boot实战:优雅构建责任链模式投诉处理业务
    • 场景描述:
    • 代码实现
      • Step 1: 定义投诉请求类
      • Step 2: 定义抽象处理者类
      • Step 3: 实现具体的处理者类
      • Step 4: 创建投诉处理链
      • Step 5: 创建控制器处理请求
    • 测试:通过 API 提交投诉
    • 责任链模式解决的问题
    • 总结

责任链模式 是一种行为设计模式,它允许多个对象来处理请求,而不预先指定具体的处理者。多个处理对象被连接成一条链,沿着这条链传递请求,直到某个处理对象决定处理这个请求为止。责任链模式通过将请求的发送者与接收者解耦,来提高系统的灵活性。

责任链模式原理解析:点击阅读

场景描述:

在日常开发中,请求处理链审批流程是典型的责任链模式应用场景。这里我们模拟一个场景:客户投诉处理系统。系统处理客户的投诉请求,根据投诉的严重程度,投诉会被逐级传递到不同的处理人员:

  1. 客服代表(Customer Service Representative):处理一般的轻度投诉(例如:商品延迟到达)。
  2. 客服主管(Customer Service Supervisor):处理中度投诉(例如:产品损坏)。
  3. 客服经理(Customer Service Manager):处理严重投诉(例如:客户服务态度问题,涉及到重大业务问题)。

如果某一级无法处理投诉,则投诉将沿着责任链传递给更高一级的处理者。

代码实现

我们使用 Spring Boot 来实现这个责任链模式。每个处理者(处理人员)都有自己能处理的投诉类型和级别,如果无法处理,则交给下一个处理者

Step 1: 定义投诉请求类

我们先定义一个 ComplaintRequest 类来封装客户的投诉请求,包含投诉类型、严重程度和投诉内容。

// 请求类:投诉请求
public class ComplaintRequest {private String customerName;private String complaintType;private int severityLevel;  // 投诉严重程度(1:轻度,2:中度,3:重度)private String complaintDetails;public ComplaintRequest(String customerName, String complaintType, int severityLevel, String complaintDetails) {this.customerName = customerName;this.complaintType = complaintType;this.severityLevel = severityLevel;this.complaintDetails = complaintDetails;}public String getCustomerName() {return customerName;}public String getComplaintType() {return complaintType;}public int getSeverityLevel() {return severityLevel;}public String getComplaintDetails() {return complaintDetails;}
}

Step 2: 定义抽象处理者类

所有处理人员都会实现 ComplaintHandler 接口。这个接口定义了 setNext() 方法来设置下一个处理者,以及 handleComplaint() 方法来处理投诉。

// 抽象处理者接口:投诉处理者
public abstract class ComplaintHandler {protected ComplaintHandler nextHandler;// 设置下一个处理者public void setNext(ComplaintHandler nextHandler) {this.nextHandler = nextHandler;}// 处理投诉的抽象方法public abstract void handleComplaint(ComplaintRequest request);
}

Step 3: 实现具体的处理者类

CustomerServiceRepresentativeHandler

CustomerServiceRepresentativeHandler 只能处理轻度投诉(严重程度为 1 的投诉)。

// 具体处理者:客服代表
import org.springframework.stereotype.Component;@Component
public class CustomerServiceRepresentativeHandler extends ComplaintHandler {@Overridepublic void handleComplaint(ComplaintRequest request) {if (request.getSeverityLevel() == 1) {System.out.println("Customer Service Representative handled the complaint: " + request.getComplaintDetails());} else {if (nextHandler != null) {nextHandler.handleComplaint(request);}}}
}

CustomerServiceSupervisorHandler

CustomerServiceSupervisorHandler 可以处理中度投诉(严重程度为 2 的投诉)。

// 具体处理者:客服主管
import org.springframework.stereotype.Component;@Component
public class CustomerServiceSupervisorHandler extends ComplaintHandler {@Overridepublic void handleComplaint(ComplaintRequest request) {if (request.getSeverityLevel() == 2) {System.out.println("Customer Service Supervisor handled the complaint: " + request.getComplaintDetails());} else {if (nextHandler != null) {nextHandler.handleComplaint(request);}}}
}

CustomerServiceManagerHandler

CustomerServiceManagerHandler 负责处理严重投诉(严重程度为 3 的投诉)。

// 具体处理者:客服经理
import org.springframework.stereotype.Component;@Component
public class CustomerServiceManagerHandler extends ComplaintHandler {@Overridepublic void handleComplaint(ComplaintRequest request) {if (request.getSeverityLevel() == 3) {System.out.println("Customer Service Manager handled the complaint: " + request.getComplaintDetails());} else {System.out.println("Complaint cannot be handled.");}}
}

Step 4: 创建投诉处理链

接下来,我们创建一个配置类来设置投诉处理链的顺序。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ComplaintHandlerConfig {@Beanpublic ComplaintHandler complaintHandler(CustomerServiceRepresentativeHandler repHandler,CustomerServiceSupervisorHandler supervisorHandler,CustomerServiceManagerHandler managerHandler) {// 设置责任链顺序:客服代表 -> 客服主管 -> 客服经理repHandler.setNext(supervisorHandler);supervisorHandler.setNext(managerHandler);return repHandler;  // 返回链条的第一个处理者}
}

Step 5: 创建控制器处理请求

我们通过 Spring Boot 控制器来模拟投诉请求的提交,控制器将投诉请求传递给责任链的第一个处理者。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/complaint")
public class ComplaintController {private final ComplaintHandler complaintHandler;@Autowiredpublic ComplaintController(ComplaintHandler complaintHandler) {this.complaintHandler = complaintHandler;}@PostMapping("/submit")public String submitComplaint(@RequestParam String customerName,@RequestParam String complaintType,@RequestParam int severityLevel,@RequestParam String complaintDetails) {// 创建投诉请求ComplaintRequest request = new ComplaintRequest(customerName, complaintType, severityLevel, complaintDetails);// 将投诉请求传递给责任链complaintHandler.handleComplaint(request);return "Complaint submitted!";}
}

测试:通过 API 提交投诉

我们可以通过 Postman 或 curl 来测试这个投诉处理系统。假设服务器运行在 localhost:8080

1.提交轻度投诉(严重程度为 1)

curl -X POST "http://localhost:8080/complaint/submit?customerName=John&complaintType=Delivery&severityLevel=1&complaintDetails=Package delayed"

输出结果

Customer Service Representative handled the complaint: Package delayed

2.提交中度投诉(严重程度为 2)

curl -X POST "http://localhost:8080/complaint/submit?customerName=Jane&complaintType=Product&severityLevel=2&complaintDetails=Product damaged"

输出结果

Customer Service Supervisor handled the complaint: Product damaged

3. 提交严重投诉(严重程度为 3)

curl -X POST "http://localhost:8080/complaint/submit?customerName=Alice&complaintType=Service&severityLevel=3&complaintDetails=Bad customer service experience"

输出结果

Customer Service Manager handled the complaint: Bad customer service experience

责任链模式解决的问题

  1. 职责解耦
    • 每个处理者只负责自己权限范围内的投诉,无法处理的请求会自动传递给下一个处理者。这样可以有效解耦请求发送者与接收者。
  2. 灵活扩展
    • 新增处理者非常简单,可以通过调整链条顺序或添加新的处理者来扩展系统功能,符合开闭原则
  3. 避免复杂的条件判断
    • 通过责任链模式,避免了在一个类中使用大量的 if-else 判断请求的复杂性。每个处理者只处理自己关心的请求,其他请求交给下一个处理者。

总结

通过 Spring Boot 实现的责任链模式,在客户投诉处理系统中清晰展示了如何将职责链条化。不同的处理人员各自负责不同等级的投诉请求,系统根据请求的严重程度将其逐级传递,直到找到合适的处理者。通过这种方式,我们不仅简化了处理逻辑,还提高了系统的可扩展性和灵活性。这种模式在实际开发中非常适用于类似审批流程、事件处理、异常处理等场景。


http://www.mrgr.cn/news/35828.html

相关文章:

  • Java 反射体系
  • 【伺服】Servo入坑学习记录①
  • 图解Transformer就这30页PPT,你们真不看啊
  • 双端搭建个人博客
  • Vue3 tsx文件中如何实现页面跳转
  • sql server 官方学习网站
  • vue3腾讯云实时音视频通话 ui集成方案TUIcallkit
  • 编码器分辨率、精度和重复精度的定义
  • 线性判别分析 (LDA)中目标函数的每个部分的具体说明
  • 【P1320 压缩技术(续集版)】
  • 优化理论及应用精解【11】
  • Prompt输出限制怎么写?用CCoT限制输出长度的推理,大幅提高LLM准确性
  • 在pycharm中怎样调试HTML网页程序
  • C语言课程设计题目二:图书信息管理系统设计
  • vulnhub靶场Matrix-win全流程
  • 【设计模式-策略】
  • 双十一有哪些好物值得入手?五款超值数码好物分享!
  • C# 用统一代码动态查询数据库并显示数据
  • 芒果TV《航海少年团》强强联合,优质少儿动画乘风起航
  • W39-02-jmeter中如何实现:下一个请求是需要根据前一个请求返回值进行循环请求