Design Patterns · 8 min read · Beginner
Adapter & Decorator Patterns
Bridge incompatible APIs with Adapters and layer cross-cutting behavior (logging, caching) cleanly with Decorators.
What you'll learn
- Wrap a third-party API behind your own interface using the Adapter pattern
- Layer behavior dynamically around an object using the Decorator pattern
- Avoid subclass explosion when combining orthogonal features
- Adapter translates an external or legacy interface into the clean contract your app expects (like a power plug adapter).
- Decorator wraps an object implementing the same interface to add extra behavior (logging, timing, retries, caching) without modifying the original class.
Fill-in-the-Blank Exercise
Logging Decorator
Wrap an inner service, log the call, and delegate to inner.fetch(id).
class LoggingDecorator {
constructor(inner) { this.inner = inner; }
fetch(id) {
console.log("Fetching", id);
return this..(id);
}
}Live OOP Playground
Composable Caching Decorator
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. What is the key difference between an Adapter and a Decorator?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.