← Back to PatternPip

DECORATOR / THE IDEA IN A MINUTE

One job.
A few optional extras.

Decorator adds behavior to an object by wrapping it in another object with the same interface.

The client keeps its familiar call. Each wrapper adds a small responsibility before or after delegating to the object inside it. Wrappers can be combined without a separate class for every combination.

What changes?
Optional behavior around an existing object.
What stays the same?
The interface the client calls: send(message).
How do extras combine?
Each decorator wraps another Sender and delegates to it.

THE STRUCTURE / FOUR ROLES

A wrapper is a Sender. It also holds a Sender.

That is what makes Decorator work: the client can call the wrapper through the same interface, and the wrapper passes the job to the object inside it.

Choose the extras

Change the wrappers. Keep the client, interface and core sender.

01 / CLIENTUnchanged

notify(sender, message)

Uses the Sender interface. It calls sender.send(message) without inspecting which wrappers were chosen.

02 / COMPONENT INTERFACEShared

Sender

send(message): Receipt

The common contract for the core object and every wrapper.

03 / CONCRETE COMPONENTUnchanged

EmailSender

Implements Sender. Does the core job: accepts the message and returns a receipt in this local example.

It does not know which wrappers surround it.

04 / DECORATORSChosen extras

LoggingSender

Records the attempt before delegating, then logs success or failure.

Each implements Sender and holds inner: Sender. Calls inner.send(message) once.

The arrows above show interface relationships. The nested objects below show composition: a wrapper holds a reference to another Sender.

THE OBJECTS AT RUNTIME

Start outside. Delegate inward.

Setup gives the client the outermost object. Each box holds the object nested inside it; all boxes support send(message).

The same receipt returns outward. If sending fails, the error propagates outward instead.

LoggingSenderWRAPPER OBJECT
send(message): Receipt

Adds logging before the call and on success or failure.

inner: Sender holds EmailSender↓ Call inner.send(message) · result returns ↑

EmailSenderCORE OBJECT
send(message): Receipt

Accepts the message once and returns its receipt.

LoggingSender wraps EmailSender. The client still calls send(message).

Now try the demo below: send a message and watch the call travel inward and the result return outward.

ONE CALL, OPTIONAL EXTRAS

Wrap the sender. Step inside each layer.

Try it yourself

This is an offline notification simulator. Tracking records local acceptance, not delivery or a read confirmation.

Optional wrappers

Each wrapper holds the object inside it.

These are separate objects, all implementing Sender. Adding or removing a wrapper changes the nesting; it does not change EmailSender.

CLIENT / OUTSIDE THE WRAPPERS

notify(sender, message)

sender refers to LoggingSender.

The client calls sender.send(message). It only needs the Sender interface.

Ready to trace

Choose your wrappers above, then start the call trace. Each step waits for you.

The highlighted object is handling the current call or result. Changes above clear the trace.

LoggingSenderWRAPPER OBJECT
send(message): Receipt

Adds logging before the call and on success or failure.

inner: Sender holds TrackingSender↓ Call inner.send(message) · result returns ↑

TrackingSenderWRAPPER OBJECT
send(message): Receipt

Records acceptance after a successful return; no receipt means no acceptance record.

inner: Sender holds EmailSender↓ Call inner.send(message) · result returns ↑

EmailSenderCORE OBJECT
send(message): Receipt

Accepts the message once and returns its receipt.

No receipt yetThe result appears after the call returns to the client.
Recorded call sequence · 0 events

Start a run to see the actual order of calls and side effects.

Choose your extras, then send a local sample message.

Try this: trace both wrappers, then swap their order. Watch the boxes change places: the outermost wrapper starts first and finishes last.

The trace replays real synchronous events. Next and Previous let you inspect each object at your own pace. Each run starts a fresh local sender. No real messages are sent.

Useful for independent extras.

Logging, measurements and similar optional policies can surround a service while its core job stays focused. Each chosen wrapper is a real object in the call chain.

Keep the layers understandable.

Too many wrappers make behavior harder to trace. A function or a few stable options may be simpler. Choose order deliberately and preserve the wrapped object’s contract.

Choose your next step.