Multiple choice technology architecture

Example of Decorator pattern in Java API?

  1. java.util.List

  2. java.lang.String

  3. java.io.BufferedInputStream.

  4. None of the above.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The Decorator pattern attaches additional responsibilities to an object dynamically. In Java's I/O streams, BufferedInputStream decorates an underlying InputStream to add buffering functionality. This allows adding behavior to individual objects without affecting other objects of the same class.

AI explanation

To answer this question, let's go through each option to understand if it represents an example of the Decorator pattern in the Java API:

Option A) java.util.List - This option is incorrect. The List interface in Java represents an ordered collection of elements and does not implement the Decorator pattern.

Option B) java.lang.String - This option is incorrect. The String class in Java represents a sequence of characters and does not implement the Decorator pattern.

Option C) java.io.BufferedInputStream - This option is correct. The BufferedInputStream class in the java.io package is an example of the Decorator pattern in the Java API. It provides additional functionality to an input stream by buffering the input and improving performance.

Option D) None of the above - This option is incorrect. Option C, java.io.BufferedInputStream, is an example of the Decorator pattern in the Java API.

The correct answer is: C) java.io.BufferedInputStream. This option represents an example of the Decorator pattern in the Java API because it adds functionality to an existing input stream by buffering the input.