Structural pattern · Visual logic
Decorator Pattern: Add Capability in Layers
Decorator wraps an object with optional layers, allowing capabilities to be combined at runtime without creating a subclass for every combination.
Optional features multiply into a subclass explosion because every possible combination requires another specialized type.
Wrap the same interface with focused decorators that add one responsibility before or after delegation.
- BASE OBJECT provides the smallest valid behavior behind one interface.
- DECORATORS wrap that interface and add a focused responsibility.
- CALLS flow through the selected layers and finally reach the base.
Features become composable layers rather than a rigid inheritance tree full of combinations.
Run it in the real world
A notification begins as plain text, then gains encryption, compression, and logging only where those layers are required.
Go deeper
This is an original explanation and example inspired by Head First Design Patterns. The book covers the complete pattern, design principles, and implementation detail.
Retail links are not affiliate links yet. If that changes, this page will clearly disclose it.