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?
- A. Decorator
- B. Adapter
- C. Factory Method
- 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
- Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any…
- Square extends Rectangle and overrides setWidth and setHeight so that both always change together. Client code written for Rectangle does…
- A Department has Professors who continue to exist if the department is closed, while a House is made of Rooms that cannot exist without…
- What is printed? java // static methods of the main class: static void p(Object o) { System.out.print("O"); } static void p(String s) {…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2)); for (Map.Entry<String, Integer> e : m.entrySet())…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2, "c", 3)); m.keySet().remove("b")…