Chain of Responsibility
Context: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain.
public abstract class Handler{ protected Handler _next; public void SetNext(Handler next) => _next = next; public abstract void HandleRequest(int request);}
public class ConcreteHandlerA : Handler{ public override void HandleRequest(int request) { if (request < 10) Console.WriteLine("Handler A handled"); else _next?.HandleRequest(request); }}Real-world usage example
Section titled “Real-world usage example”Logging framework: Loggers with different levels (INFO, DEBUG, ERROR) form a chain. If a logger cannot handle a level, it passes to the next.
Example: ASP.NET Core middleware pipeline is a chain of responsibility. Each middleware decides to process or pass to the next. Also, ValidationHandler in FluentValidation.