Repaso
Español
Design & PracticesIntermediateDP-48

Servlet filters and Spring Security's filter chain are examples of which pattern?

Options

  1. Chain of Responsibility: each handler processes or forwards the request (Correct option)
  2. Prototype: each filter makes a copy of the incoming request
  3. Flyweight: all the filters share a single request object
  4. Memento: the filters save the state of the request and restore it

Answer

Chain of Responsibility: each handler processes the request or passes it to the next one

Why

In Chain of Responsibility, a request goes through a sequence of handlers. Each can handle it, modify it, stop it, or pass it on.

In a web app: logging filter, authentication filter, CORS filter, then the controller. If authentication fails, the chain stops and returns 401.

Other examples: middleware in Express/FastAPI, approval workflows.

Remember

Chain of Responsibility = pipeline of handlers.

Key terms

Practice this question in Repaso