【设计模式】责任链模式

责任链模式(Chain of Responsibility Pattern)

概念:

· 一种行为型设计模式;

· 核心思想是将请求的发送者和接收者解耦,让多个对象都有机会处理该请求,将这些对象连成一条链,并沿着这条链子传递请求,直到有对象能够处理这个请求为止

UML结构:

复制代码
       Handler (抽象处理者)
        + setNext(handler)
        + handleRequest(request)
               |
    -------------------------
    |           |           |
ConcreteHandler1 ConcreteHandler2 ConcreteHandler3
        |
      Client

代码示例:

cs 复制代码
/// <summary>
/// 抽象处理者
/// </summary>
public abstract class Employee
{
    protected Employee _next; // 上级处理者
    protected int _canApproval; // 可审批天数
    protected string _name;

    public Employee(string name, int canApproval)
    {
        _name = name;
        _canApproval = canApproval;
    }

    public void SetNext(Employee next)
    {
        _next = _next;
    }

    public void HandleRequest(int days)
    {
        if (days <= _canApproval)
        {
            Console.WriteLine($"{_name}审批成功,请假天数:{days}");
        }
        else if (_next != null)
        {
            Console.WriteLine($"{_name}无法审批,请求上报{_next._name}");
        }
        else
        {
            Console.WriteLine("审批失败,无人可处理!");
        }
    }
}

/// <summary>
/// 普通员工类(处理者1)
/// </summary>
public class OrdinaryEmployee : Employee
{
    public OrdinaryEmployee(string name, int canApproval) : base(name, canApproval) { }
}

/// <summary>
/// 部门经理类(处理者2)
/// </summary>
public class DepartmentManager : Employee
{
    public DepartmentManager(string name, int canApproval) : base(name, canApproval) { }
}

/// <summary>
/// 总经理类(处理者3)
/// </summary>
public class GeneralManager : Employee
{
    public GeneralManager(string name, int canApproval) : base(name, canApproval) { }
}

/// <summary>
/// 客户端
/// </summary>
public class Client
{
    public static void Main()
    {
        Employee generalManager = new GeneralManager("总经理", 30);
        Employee departmentManager = new DepartmentManager("部门经理", 5);
        Employee ordinaryEmployee = new OrdinaryEmployee("普通员工", 0);

        ordinaryEmployee.SetNext(departmentManager);
        departmentManager.SetNext(generalManager);

        ordinaryEmployee.HandleRequest(2);

        ordinaryEmployee.HandleRequest(10);

        departmentManager.HandleRequest(35);
    }
}

特点:
优点:

· 降低耦合度:请求发送者无需知道请求由谁处理;

· 增强灵活性:可动态调整责任链顺序或新增处理者;

· 符合单一职责原则:每个处理者只处理自己职责范围内的请求;
缺点:

· 可能产生链过长导致影响了性能;

· 请求可能到达末端都无人处理,因此需要额外的判断;

· 调试和跟踪困难,沿着链传递时,容易不清楚实际的处理者是谁;

适用场景:

· 请求有多个处理对象,但是不确定应该被哪一个处理(审批流程、事件处理、过滤器链);

· 希望动态处理顺序或增加新的处理者;

· 处理职责明确,各个执行处理者可分离;

举例:

· 公司请假审批;

· 日志处理系统(DEBUG、INFO、ERROR 级别日志交给不同日志处理器处理);

· Web请求过滤器链(身份认证 → 参数校验 → 请求记录 → 响应);

相关推荐
傻啦嘿哟6 分钟前
Python上下文管理器:优雅处理资源释放的魔法工具
开发语言·python
阿方索7 分钟前
Python 基础简介
开发语言·python
Data_agent9 分钟前
CNFANS模式淘宝1688代购系统搭建指南
大数据·开发语言·前端·javascript
Sammyyyyy16 分钟前
MongoDB 的文档模型与 CRUD 实战
开发语言·数据库·mongodb·servbay
CodeCraft Studio16 分钟前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建折线图
java·c#·excel·aspose.cells·excel图表·excel api库·excel折线图
帅那个帅17 分钟前
go的雪花算法代码分享
开发语言·后端·golang
挖矿大亨22 分钟前
C++中的引用
开发语言·c++
程序员阿鹏26 分钟前
事务与 ACID 及失效场景
java·开发语言·数据库
趁月色小酌***44 分钟前
JAVA 知识点总结2
java·开发语言