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
TypeScript example
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.