设计模式--职责链模式

职责链模式(Chain of Responsibility Pattern)是一种行为设计模式,它允许多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。

职责链模式的结构

职责链模式的结构包括以下几个部分:

  1. 抽象处理者(Handler):定义一个处理请求的接口,包含处理请求的方法和设置后继处理者的方法。
  2. 具体处理者(ConcreteHandler):实现抽象处理者的接口,处理它所负责的请求。如果不能处理请求,就将请求转发给后继者。
  3. 客户端(Client):向链上的具体处理者对象提交请求。

举例:客户投诉处理系统

假设我们有一个客户投诉处理系统,不同级别的管理者(如客服代表、客服主管、客服经理)处理不同级别的投诉。如果一个投诉超出了某个管理者的处理范围,他会将投诉转交给更高一级的管理者。

1. 抽象处理者(Handler)
// 抽象处理者,定义处理请求的接口
abstract class ComplaintHandler {
    // 后继处理者
    protected ComplaintHandler nextHandler;

    // 设置后继处理者的方法
    public void setNextHandler(ComplaintHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 处理请求的抽象方法,具体处理者需要实现
    public abstract void handleComplaint(String complaint, int severity);
}

 2. 具体处理者(ConcreteHandler)

// 客服代表处理者
class CustomerServiceRepresentative extends ComplaintHandler {
    @Override
    public void handleComplaint(String complaint, int severity) {
        // 如果投诉的严重级别小于等于1,则处理该投诉
        if (severity <= 1) {
            System.out.println("Customer Service Representative handling complaint: " + complaint);
        } else if (nextHandler != null) {
            // 否则将投诉转发给下一个处理者
            nextHandler.handleComplaint(complaint, severity);
        }
    }
}

// 客服主管处理者
class CustomerServiceSupervisor extends ComplaintHandler {
    @Override
    public void handleComplaint(String complaint, int severity) {
        // 如果投诉的严重级别小于等于2,则处理该投诉
        if (severity <= 2) {
            System.out.println("Customer Service Supervisor handling complaint: " + complaint);
        } else if (nextHandler != null) {
            // 否则将投诉转发给下一个处理者
            nextHandler.handleComplaint(complaint, severity);
        }
    }
}

// 客服经理处理者
class CustomerServiceManager extends ComplaintHandler {
    @Override
    public void handleComplaint(String complaint, int severity) {
        // 如果投诉的严重级别小于等于3,则处理该投诉
        if (severity <= 3) {
            System.out.println("Customer Service Manager handling complaint: " + complaint);
        } else {
            // 否则无法处理该投诉,打印提示信息
            System.out.println("Complaint: " + complaint + " is too severe to be handled.");
        }
    }
}

3. 客户端代码(Client)

public class ComplaintSystem {
    public static void main(String[] args) {
        // 创建处理者对象
        ComplaintHandler representative = new CustomerServiceRepresentative();
        ComplaintHandler supervisor = new CustomerServiceSupervisor();
        ComplaintHandler manager = new CustomerServiceManager();

        // 设置职责链
        representative.setNextHandler(supervisor);
        supervisor.setNextHandler(manager);

        // 提交投诉
        representative.handleComplaint("Minor issue", 1);
        representative.handleComplaint("Moderate issue", 2);
        representative.handleComplaint("Serious issue", 3);
        representative.handleComplaint("Critical issue", 4);
    }
}
运行结果

Customer Service Representative handling complaint: Minor issue
Customer Service Supervisor handling complaint: Moderate issue
Customer Service Manager handling complaint: Serious issue
Complaint: Critical issue is too severe to be handled.

代码说明

  1. 抽象处理者

    • ComplaintHandler 是一个抽象类,定义了处理投诉请求的接口和设置后继处理者的方法。
    • handleComplaint 方法是一个抽象方法,由具体处理者类实现。
  2. 具体处理者

    • CustomerServiceRepresentativeCustomerServiceSupervisorCustomerServiceManager 是具体的处理者,分别处理不同级别的投诉。
    • 每个具体处理者都有一个处理请求的方法 handleComplaint,根据投诉的严重级别进行处理。如果不能处理请求,则将请求转发给下一个处理者。
  3. 客户端代码

    • ComplaintSystem 类是客户端,创建了处理者对象,并设置了职责链。
    • 调用 handleComplaint 方法提交投诉,根据投诉的严重级别由相应的处理者处理。

通过这个示例代码,可以看到职责链模式是如何通过一系列处理者对象来处理请求的,每个处理者都有机会处理请求,如果不能处理则传递给下一个处理者,从而实现请求的灵活处理和解耦。

相关推荐

  1. 设计模式 -职责模式

    2024-07-22 07:16:03       55 阅读
  2. 设计模式--职责模式

    2024-07-22 07:16:03       18 阅读
  3. 设计模式的应用——《职责模式

    2024-07-22 07:16:03       44 阅读
  4. 行为型设计模式职责模式

    2024-07-22 07:16:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 07:16:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 07:16:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 07:16:03       45 阅读
  4. Python语言-面向对象

    2024-07-22 07:16:03       55 阅读

热门阅读

  1. PXIe-6592

    PXIe-6592

    2024-07-22 07:16:03      13 阅读
  2. FPGA 中的 IOE与IO BANK

    2024-07-22 07:16:03       18 阅读
  3. 前端部署后提示用户刷新页面

    2024-07-22 07:16:03       16 阅读
  4. 编写测试用例:策略、技巧与最佳实践

    2024-07-22 07:16:03       17 阅读
  5. 自动化测试的艺术:Xcode中GUI测试的全面指南

    2024-07-22 07:16:03       17 阅读
  6. C++基础语法:STL之容器(6)--序列容器中的forward_list

    2024-07-22 07:16:03       15 阅读
  7. MongoDB Map-Reduce 简介

    2024-07-22 07:16:03       15 阅读
  8. 【SpringBoot】第3章 SpringBoot的系统配置

    2024-07-22 07:16:03       15 阅读
  9. Python中with 关键字、tell() 和 seek() 方法

    2024-07-22 07:16:03       17 阅读
  10. 初识数据结构中的“栈”

    2024-07-22 07:16:03       16 阅读
  11. 44、PHP 实现数据流中的中位数(含源码)

    2024-07-22 07:16:03       16 阅读
  12. Python面试题:Python中的单例模式及其实现

    2024-07-22 07:16:03       18 阅读
  13. JVM 中的OopMap与安全点

    2024-07-22 07:16:03       17 阅读