01 / CLIENTUnchanged
notify(sender, message)
Uses the Sender interface. It calls sender.send(message) without inspecting which wrappers were chosen.
Appearance
DECORATOR / THE IDEA IN A MINUTE
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.
THE STRUCTURE / FOUR ROLES
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.
Change the wrappers. Keep the client, interface and core sender.
01 / CLIENTUnchanged
Uses the Sender interface. It calls sender.send(message) without inspecting which wrappers were chosen.
02 / COMPONENT INTERFACEShared
send(message): ReceiptThe common contract for the core object and every wrapper.
03 / CONCRETE COMPONENTUnchanged
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
Records the attempt before delegating, then logs success or failure.
Each implements Sender and holds inner: Sender. Calls inner.send(message) once.
THE OBJECTS AT RUNTIME
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.
send(message): ReceiptAdds logging before the call and on success or failure.
inner: Sender holds EmailSender↓ Call inner.send(message) · result returns ↑
send(message): ReceiptAccepts 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
This is an offline notification simulator. Tracking records local acceptance, not delivery or a read confirmation.
These are separate objects, all implementing Sender. Adding or removing a wrapper changes the nesting; it does not change EmailSender.
CLIENT / OUTSIDE THE WRAPPERS
sender refers to LoggingSender.
The client calls sender.send(message). It only needs the Sender interface.
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.
send(message): ReceiptAdds logging before the call and on success or failure.
inner: Sender holds TrackingSender↓ Call inner.send(message) · result returns ↑
send(message): ReceiptRecords acceptance after a successful return; no receipt means no acceptance record.
inner: Sender holds EmailSender↓ Call inner.send(message) · result returns ↑
send(message): ReceiptAccepts the message once and returns its receipt.
Start a run to see the actual order of calls and side effects.
Choose your extras, then send a local sample message.
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.
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.
Start with one sender, feel the cost of mixing optional jobs, then wrap each responsibility separately.
Three chapters · code on demand · practiceBACK FOR A REFRESHERReview the roles, composition order, delegation contract and failure behavior.
TypeScript + Kotlin · bookmarkable