Design patterns · interview question
new BufferedInputStream(new FileInputStream(file)) adds buffering to a stream by wrapping it. Which pattern is this?
Multiple choiceMedium
Answer and explanation
Correct answer
A. Decorator
Why
A Decorator implements the same interface as the object it wraps and adds behavior, and decorators can be stacked.
References
- Gamma, Helm, Johnson & Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (1994)
Test yourself on Design patterns
A free test with instant, explained results. No account needed.
More Design patterns questions
- Explain the Strategy pattern with a concrete Java example. When would you use it?
- A code review describes a ConfigManager singleton. Highlight the statements that describe real problems.
- Your checkout flow wants one clean charge(order) call, but each payment provider ships a different SDK. Which design…
- Which principle is most closely aligned with the Factory Pattern?
- Which scenario is least suited for the Decorator pattern?
- Why is it important for a decorator to share the same interface as the object it decorates?