Decorator pattern
medium 45 minDecorator adds behaviour to one object by wrapping it in another with the same interface.
Watch lesson4:34
Where it shows up
The same pattern appears across everyday software.
Java I/O
`FilterInputStream` wraps some other input stream and may transform data or add functionality. BufferedInputStream, DataInputStream and others extend it.
Java Collections
`Collections.unmodifiableList` and `synchronizedList` return views around a given list: same interface, one behaviour added.
Python functools
`lru_cache` wraps a function with a memo of recent calls. `wraps` copies name and documentation to the wrapper and stores the original in `__wrapped__`.
Express middleware
Functions with request, response and next. Each can act, change the objects, end the cycle or call the next one. The order in which they are loaded matters.
ASP.NET Core middleware
Each component chooses whether to pass the request on, and can work before and after the next one. The documentation lists a required order.
Django middleware
Described as layers of an onion around the view: the request passes inwards through every layer, the response passes back outwards.
Go net/http
`TimeoutHandler` returns a Handler that runs another Handler with a time limit. `StripPrefix` wraps a handler too.
Resilience4j
Higher-order functions, called decorators, enhance a function with a circuit breaker, rate limiter, retry or bulkhead, and they can be stacked.