Servlet filters and Spring Security's filter chain are examples of which pattern?
Options
- Chain of Responsibility: each handler processes or forwards the request (Correct option)
- Prototype: each filter makes a copy of the incoming request
- Flyweight: all the filters share a single request object
- 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.