责任链模式
The Chain of Responsibility design pattern avoids coupling the sender of the request to its receiver by giving more than one object a chance to handle the request. This pattern chains the receiving objects and passes the request along the chain until an object handles it.
责任链模式避免发送者和接受者耦合,给每个对象机会处理请求,此模式将接收对象连接起来,并沿着该链传递请求直到处理它为止。
In simple words, we can say that the chain of responsibility desgin pattern creates a chain of receiver objects for given request. In this design pattern, normally each receiver contains a reference to another receiver. If one receiver cannot handle the request then it passes the same request to the next receiver and so on.One receiver hanldes the request in the chain or one or more receivers handle the request.
UML Calss Diagram
Handler: this is going to be an abstract class that deines how the request is going to be handled. It contains a member that holds the next handler in the chain and an associated method to set thhis next handler. It also has an abstract method that is going to be implemented by concrete handler classes to handle the incoming request as well as if required then it will pass the request to the next handler object in the pipeline.
ConcreteHandler: this is going to be concrete classes that is inherited from handler abstract class and provide implementations for the abstract method. This method has the logic to handle the request. And if required, then it will forward the request to the next handler associated tin the pipeline.
Client: This is the class that generates the request and passes it to the first handler in the chain of responsibility.
Structure Code in C#

public abstract class Handler { protected Handler nextHandler; public void SetNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } public abstract void HandleRequest(int request); }

public class ConcreteHanderA : Handler { public override void HandleRequest(int request) { if (request >= 0 && request < 10) Console.WriteLine($"{this.GetType().Name} handled request {request}."); else if (nextHandler != null) nextHandler.HandleRequest(request); } } public class ConcreteHanderB : Handler { public override void HandleRequest(int request) { if (request >= 10 && request < 20) Console.WriteLine($"{this.GetType().Name} handled request {request}."); else if (nextHandler != null) nextHandler.HandleRequest(request); } } public class ConcreteHanderC : Handler { public override void HandleRequest(int request) { if (request >= 20 && request < 30) Console.WriteLine($"{this.GetType().Name} handled request {request}."); else if (nextHandler != null) nextHandler.HandleRequest(request); } }
When to use Chain of Responsibility Design Pattern in Real-Time Application?
- A set of handlers are used in the pipeline to handle the request.
- You need to pass a request to one handler at run-time based on certain conditions.
- Exception Handling in C# is one of the best example of the chain for responsibility design pattern.When an exception is thrown from the try block, then that excpetion might be going to handle by a corresponding catch block. Here, you can have more than one catch block. Here the catch blocks will behave like handlers to handle exception.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)