Decorator Pattern Explained
Add behavior to objects dynamically by wrapping them in decorator layers — composing functionality without modifying existing code or creating subclass explosions.
Decorator Pattern
The Decorator pattern is a structural design pattern that attaches additional responsibilities to an object dynamically by wrapping it in a decorator object that implements the same interface.
Explanation
The Decorator pattern adds behavior to individual objects without affecting other objects of the same class. Each decorator wraps the original object and adds functionality before or after delegating to the wrapped object's methods. Multiple decorators can be stacked, each adding a layer of behavior. This pattern is an alternative to subclassing: instead of creating subclasses for every combination of features (LoggingService, CachingService, LoggingCachingService...), you create individual decorators and compose them as needed. This follows the Open/Closed Principle — extending behavior without modifying existing code. Real-world examples include middleware chains (Express.js middleware decorates request handling), I/O streams (Java's BufferedReader decorates FileReader), logging decorators (wrap any service to add logging), caching decorators (wrap any data source to add caching), and retry decorators (wrap any API call to add retry logic). TypeScript/Python decorators (@decorator syntax) are inspired by but distinct from the Gang of Four pattern.
Bookuvai Implementation
Bookuvai uses the Decorator pattern for cross-cutting concerns: logging, caching, retry logic, and metrics collection. Our middleware architectures compose decorators to add behavior without modifying core services. TypeScript decorators handle metadata and dependency injection annotations.
Key Facts
- Adds behavior dynamically by wrapping objects with the same interface
- Alternative to subclassing — compose behaviors instead of inheriting them
- Multiple decorators can be stacked for layered functionality
- Follows the Open/Closed Principle — extend without modifying
- Common uses: logging, caching, retry, authorization, and metrics
Related Terms
Frequently Asked Questions
- Are TypeScript decorators the same as the Decorator pattern?
- No. TypeScript/Python decorators (@decorator syntax) are a language feature for annotating classes and methods with metadata. The Gang of Four Decorator pattern is a structural design that wraps objects to add behavior. They share the name but are different concepts.
- How is Decorator different from Proxy?
- Both wrap objects with the same interface, but their intent differs. Decorator adds new behavior. Proxy controls access to the wrapped object (lazy loading, access control, logging). The structural similarity is high, but the purpose is distinct.
- When should I use Decorator vs middleware?
- Middleware is a specific application of the Decorator concept for request/response pipelines. Use the general Decorator pattern when you want to add behavior to any object type. Use middleware frameworks when processing requests through a pipeline of handlers.