Decorator

Wrapper

Escrito por Darjus Kazakevic · revisado el

Add behaviour by wrapping, one layer at a time.

Caching, retries, timing and logging are things you want around a call, not inside it. Put them inside, and the class that loads a customer is also a cache and a retry policy. Each concern makes the others harder to test.

A Decorator has the same interface as the object it wraps. It does its one extra job and delegates the rest. Because the type does not change, decorators nest, and you choose the order where you wire the app. In TypeScript a decorator is often a higher-order function: a function that takes a function and returns a wrapped one.

En esencia — Wrap an object in another with the same interface to add one behaviour, so caching, retries and logging stack in layers instead of piling up in one class.

Cuándo recurrir a él

Recurre a él cuando

  • Caching, retries or timing are creeping into the class they wrap
  • Different call sites need different mixes of the same extras
  • You cannot or should not edit the wrapped code, such as a vendor client

Reconsidéralo cuando

  • The added behaviour needs the inner object's private state
  • A wide interface where every wrapper must forward dozens of methods
  • One extra concern, used in one place: write it inline

Conceptos clave

Shared interface
The type that both the real object and every wrapper implement, such as Find = (id) => Promise<Customer>. Callers see only this type, so they cannot tell how many layers there are.
Decorator
A wrapper that holds an inner object of the same type, adds one behaviour, and delegates the rest. It does not know what is inside it.
Composition order
Where you stack the layers, usually the composition root. Order changes behaviour, so it should be visible in one place.
In modern TypeScript
For one function, a higher-order function is enough: withRetry(fn) returns a new fn. Use a class when the interface has several methods. TypeScript's @decorator syntax is a different feature that changes class definitions.

Reglas de diseño

Decide the order at the wiring site
Order changes behaviour: a timeout outside retry limits the total time, inside it limits each attempt. Stack the layers in the composition root, where the order is visible.
One job per layer, same interface
Each decorator implements exactly the type it wraps and adds one concern. A wrapper that adds methods or does two jobs can no longer be removed in a test or reordered freely.
Keep the wrapper transparent
A decorator must not change what the call means: same inputs, same result type, same errors. A cache that serves stale data after an update has changed the contract.

Errores habituales

  • Stacking in the wrong order with no test for it. A timeout inside retry limits each attempt; outside, the total. Both compile.
  • A wrapper that adds new methods, so callers depend on it and it can no longer be removed.
  • A caching layer with no expiry or invalidation, so users see old data after an update.
  • A wrapper that swallows errors (a retry that returns null at the end), which changes the contract.