3

For a school report I have to explain how the java.io.Reader package implements the Decorator pattern. I have seen multiple explanations for the java.io package (here for example) but not for the java.io.Reader package (if that's even any different). Here is what I have now, I know this design doesn't show it correctly, but I am not sure how the java.io.Reader package actually does implement it.

decorator java.io.Reader

So in this case the Reader would be the Component, the InputStreamReader the Concrete Component, the BufferedReader the Decorator and the LineNumberReader the Concrete Decorator. Again, I know this is wrong but I don't know how to correctly display it.

Mees Kluivers
  • 520
  • 2
  • 6
  • 26
  • The case is correct; you could add `<>` to BufferedReader. Personally I would not really call this an example of a decorator pattern. What argues pro: BufferedReader wraps an other Reader. And even though it is not an interface, it has LineNumberReader to make a similar diagram. You could argue that delegating to the the same interface as constructor parameter _"decorates"_. – Joop Eggen Oct 29 '17 at 01:16
  • Adding LineNumberReader is a bit farfetched, just to have a similar diagram. – Joop Eggen Oct 29 '17 at 01:23

1 Answers1

1

The diagram is correct. If you have access to Intellij (or other capable tool) you can see the UML class diagram for Java types. The below snippet is one such diagram.

enter image description here

As you can see, it agrees with yours. One thing I would consider though is BufferedReader a concrete decorator since it provides additional behavior on Reader.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • Thanks! What would you say would be a decorator instead of a concrete decorator in this case? I still dont really get the difference. – Mees Kluivers Oct 29 '17 at 01:21
  • @MeesKluivers, no problem. I would say, according to the definition, a decorator would be a class that wraps the `Reader` but doesn't add any *new* behavior or functionality. – ChiefTwoPencils Oct 29 '17 at 01:41