For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/design-patterns/behavioral/chain-of-responsibility.md.

Pass a request along a chain of handlers until one handles it. Multiple handlers can process a request. You want to decouple sender from receiver

Chain of Responsibility

Overview

Pass a request along a chain of handlers until one handles it.

When to use

  • Multiple handlers can process a request.
  • You want to decouple sender from receiver.

Java example

abstract class Handler {
    private Handler next;
    Handler link(Handler next) { this.next = next; return next; }
    void handle(String msg) {
        if (next != null) next.handle(msg);
    }
}

class ErrorHandler extends Handler {
    void handle(String msg) {
        if (msg.contains("ERROR")) {
            // handle error
            return;
        }
        super.handle(msg);
    }
}

TypeScript example

abstract class Handler {
  private next?: Handler;
  link(next: Handler): Handler { this.next = next; return next; }
  handle(msg: string): void { this.next?.handle(msg); }
}

class ErrorHandler extends Handler {
  handle(msg: string): void {
    if (msg.includes("ERROR")) return;
    super.handle(msg);
  }
}

Pros and cons

Pros:

  • Flexible handler composition.
  • Reduces coupling.

Cons:

  • Can be hard to debug.
  • Requests may go unhandled.

Common pitfalls

  • Not defining a default handler.
  • Long chains with unclear responsibility.