---
name: chain-of-responsibility
description: This skill should be used when the user asks about "chain of responsibility pattern", "when to use chain of responsibility", "implement chain", "handler chain", "middleware chain", "request pipeline", or mentions needing to pass requests through a chain of handlers.
version: 1.0.0
---

# Chain of Responsibility Pattern

## What Is It
The Chain of Responsibility pattern passes a request along a chain of handlers. Each handler decides either to process the request or pass it to the next handler in the chain. It decouples senders from receivers by giving multiple objects a chance to handle the request.

## When to Use
- **Event handling** - UI events bubbling through parent elements
- **Middleware pipelines** - HTTP request processing (authentication, logging, etc.)
- **Validation chains** - Multiple validators checking different aspects
- **Error handling** - Try handlers in sequence until one succeeds
- **Approval workflows** - Manager → Director → VP based on amount
- **Command processing** - CLI commands matched by different handlers

## When NOT to Use
- **Single handler** - No need for chain if one object handles everything
- **Order doesn't matter** - Simple delegation is enough
- **Every handler must process** - Use Observer instead
- **Performance critical** - Chain traversal adds overhead

## How to Implement

### Implementation Steps
1. Define a handler interface with a method to handle requests
2. Create a base handler that maintains reference to next handler
3. Concrete handlers implement handling logic and decide to process or pass on
4. Build the chain by linking handlers together
5. Client sends request to first handler in chain

### TypeScript Implementation

```typescript
// Handler interface
interface Handler {
  setNext(handler: Handler): Handler;
  handle(request: string): string | null;
}

// Abstract handler
abstract class AbstractHandler implements Handler {
  private nextHandler: Handler | null = null;

  setNext(handler: Handler): Handler {
    this.nextHandler = handler;
    return handler;
  }

  handle(request: string): string | null {
    if (this.nextHandler) {
      return this.nextHandler.handle(request);
    }
    return null;
  }
}

// Concrete handler
class AuthHandler extends AbstractHandler {
  handle(request: string): string | null {
    if (request === 'auth') {
      return 'Authenticated';
    }
    return super.handle(request);
  }
}
```

## Code Examples
See `examples/` directory for runnable TypeScript implementations:
- `examples/chain-of-responsibility.ts` - Basic implementation with support tickets
- `examples/chain-of-responsibility-advanced.ts` - Real-world example with HTTP middleware

## Related Patterns
- **Composite** - Chain can be implemented using Composite
- **Command** - Commands can be chained
- **Decorator** - Similar structure but Decorator adds behavior, Chain passes requests
- **Observer** - Observer broadcasts; Chain passes to one handler
