Skip to main content

What It Is

The Decorator pattern adds behavior to an object by wrapping it in another object with the same interface. Each wrapper does its extra bit and passes the call on to what it wraps.

Because wrapper and wrapped share the interface, wrappers stack in any combination — like layers of clothing.

Python's @decorator syntax is this same idea applied to functions — a wrapper around a call. This page shows the object form, where layers are chosen and stacked at run time.


Example: Wrapping a Notifier

The plain notifier sends an email. One wrapper adds logging before the send; another adds an SMS after it. The caller still sees a plain Notifier.

Layers around a core

WithLog(WithSMS(EmailNotifier())) reads inside-out: email at the core, SMS around it, logging outermost. Any layer can be added, removed, or reordered without touching the others.

Prompt
Write a Python program with the Decorator pattern: a Notifier protocol (send method), a plain EmailNotifier, a WithLog wrapper class that prints a log line before passing the call on, and a WithSMS wrapper class that also sends an SMS after. Stack the wrappers around the email notifier and send one message.
Output: complete Python program
Layers around a core

When to Use It

Use it to add optional, combinable behavior — logging, caching, retries, metrics — around a core you do not want to modify. Middleware in web servers is exactly this pattern.

Skip it when the behavior is not optional and not combinable — then it belongs inside the class itself, where it is easier to find.


Pros and Cons

Pros: behavior added without touching the core class; layers combine freely; each layer is small and testable alone.

Cons: a deep stack of wrappers is hard to debug — the call travels far before doing anything; construction lines get long.