Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Decorator Pattern

Add behaviour by wrapping an object.

The idea

A decorator has the same interface as the original and adds work before or after delegating.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Wrap the object
  2. 2Add behaviour
  3. 3Delegate to the wrapped one
  4. 4Stack several wrappers
1/4

Step by step

  1. 1Wrap the object
  2. 2Add behaviour
  3. 3Delegate to the wrapped one
  4. 4Stack several wrappers

Where it is used

Logging, compression, caching wrappers.

Watch out for

Long decorator chains are hard to debug.

Related