Skip to content
uencoder

Decorator Pattern

The decorator pattern adds responsibilities to an object dynamically without changing its class, layering behavior through a chain of wrappers as an alternative to inheritance-based extension.

Purpose

Layer functionality dynamically at runtime, extending objects more flexibly than inheritance.

Mechanism

A decorator implements the same interface as the target, holds a reference to it, and adds behavior before/after delegating calls. Multiple decorators form a wrapping chain. Java's InputStream family and Python's @decorator are typical examples.

Use cases

  • Feature layering
  • I/O stream decoration
  • Cross-cutting concerns

Related terms

References