Skip to content
Commitwise
Menu

Decorator pattern

Also called: Decorator, Wrapper

Wrapping an object in another object with the same interface to add behavior, without changing the original.

Definition

A decorator implements the same interface as the object it wraps, does its extra work (logging, caching, retrying, measuring) and delegates the real work to the wrapped object. Decorators combine freely, so each behavior is one small class instead of a subclass per combination. The order of wrapping changes behavior, and decorators apply to every method of the interface.

In practice

new BufferedReader(new InputStreamReader(stream)) adds decoding and buffering around a plain stream.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.