For an assignment, I have to find out which of the Gang of Four design pattern the classes java.io.Reader and its subclasses java.io.PushbackReader, java.io.BufferedReader and java.io.FilterReader were built with.

According to this post, the design pattern would be the Decorator Pattern. This only makes sense to me if PushbackReader, BufferedReader and FilterReader can be decorated to be used at the same time, creating effectively a BufferedPushbackFilterReader. Is that the idea?

  • Look at the constructors of those classes. Do they allow you to to stack, e.g. the Buffered nature on top of the Filter nature? Commented Jun 19, 2011 at 10:33
  • @Kilian Foth: Looks like it.
    – ryyst
    Commented Jun 19, 2011 at 10:38

2 Answers 2


Yes, you can indeed decorate them like that. Just consider the following

PushbackReader pushbackBufferedReader = new PushbackReader(
     new BufferedReader(original));

That would decorate an original reader to

  • first be buffered
  • and then enable pushback/unread functionality (still the result is buffered..)

FilterReader and Reader are base classes in the hierarchy...

  • And BufferedReader, FilterReader and PushbackReader are decorators of Reader, right?
    – ryyst
    Commented Jun 19, 2011 at 10:39
  • 1
    Yes, that's correct.
    – duffymo
    Commented Jun 19, 2011 at 10:41
  • 1
    Yes - BufferedReader make the underlying reader buffered, PushbackReader enables you to 'unread' data from the reader etc...
    – dcn
    Commented Jun 19, 2011 at 10:41

The decorator pattern applies here because BufferedReader wraps a Reader - it's still a Reader (it has all the methods of Reader), but it has more "bells and whistles" (more methods).

Here's an example showing how one class "decorates" the functionality of the one it wraps, and you can chain them up to keep decorating:

        new BufferedReader(new FileReader(new File("some.file")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.