jargon

Comparison

Decorator patternvsMiddleware

Decorator pattern

you wrapped the same interface in retries, then logging, then caching, and the caller still sees one thing.

A wrapper that implements the very same interface as the object it wraps, adding behaviour before or after delegating. Because the interface is unchanged it stacks: three decorators around one object is still that one interface, which is what separates it from adapter and facade. Order matters and is easy to get wrong — caching outside retries caches the eventual success, caching inside retries caches the failure — and that is nearly always the bug.

Full entry →

Middleware

each function takes the next one and returns a new one, so authentication wrapped logging wrapped the actual handler.

A composable wrapper around a handler that can act before it, after it, or instead of it, stacked into a pipeline. It is the decorator pattern written as functions and assembled as a chain of responsibility, which is why it is the shape every web framework converged on independently. Its recurring difficulty is order: authentication before rate limiting and rate limiting before authentication are different products.

Full entry →

Related comparisons