Chain of Responsibility
Summary
Chain of Responsibility is a behavioral design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.
This design pattern makes adding new feature a lot easier. This design pattern can be used to implement plugin system or middleware system which needs high flexibility on adding and removing features.
Sample Code
Expressjs has a middleware design using the concept of Chain of Responsibility. It's special, rather than using inheritance and calling something like next.handle()
to go to next middleware, it simply uses a next()
function to proceed. I was curious how it makes the design synchronous, as next()
is a callback function.
Recursion is used instead of a for loop, so callback function is still synchronous.
Simple Express.js Style Middleware Implementation
Expressjs Style Design with a Route Handler
Reference
How is this guide?