ProgrammingModerateProduct-company placement · Java

java.io.BufferedReader wraps another Reader, is itself a Reader, and adds buffering on top of it; such wrappers can be stacked at run time. Which design pattern is this?

  1. A. Decorator
  2. B. Adapter
  3. C. Factory Method
  4. D. Composite

Correct answer: A. Decorator

A Decorator has the same interface as the object it wraps and adds behaviour around it, so decorators can be stacked. An Adapter instead converts one interface into a different one.

More OOP with C++ and Java questions

Exams that ask this topic