Design Patterns · Junior · Question 9 of 75
What does the Decorator pattern do?
- ✓Correct answer: Adds behavior to an object or function by wrapping it
- 2Changes a component's look by attaching CSS classes to it
- 3Replaces the parent class with another class at runtime
Explanation
The wrapper implements the same interface and adds logic before or after the call: caching, logging, retries. Examples: debounce(fn), withAuth(Component) (a HOC), @ decorators in TypeScript. Wrappers can be combined.