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.
ONE CALL, OPTIONAL EXTRAS
Wrap it. Follow the call. Unwrap the result.
This is an offline notification simulator. Tracking records local acceptance, not delivery or a read confirmation.
USES SENDER
Client
send(message)Same call for every combination.
IMPLEMENTS SENDER
LoggingSender
send(message)Record attempt, then success or failure.
IMPLEMENTS SENDER
TrackingSender
send(message)Record the receipt only after acceptance.
CORE BEHAVIOR
EmailSender
send(message)Accept the message and return a receipt.
Recorded call sequence · 0 events
Send a sample to see the actual order of calls and side effects.
Choose your extras, then send a local sample message.
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.
Learn through an example
Start with one sender, feel the cost of mixing optional jobs, then wrap each responsibility separately.
Three chapters · code on demand · practiceBACK FOR A REFRESHERQuick reference
Review the roles, composition order, delegation contract and failure behavior.
TypeScript + Kotlin · bookmarkable