File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Decorator pattern within java.io Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Decorator pattern within java.io" Watch "Decorator pattern within java.io" New topic
Author

Decorator pattern within java.io

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
I know decorator pattern is used to create java.io classes. For example, Reader, FileReader, BufferedReader, which combine together showing a Decorator structure. But Let's see the code in java tutorial

Does the code above diplay how is Decorator used? I don't think so. The code below is what I think how Decorator should be used

Anyone help me to clarify that? Thanks.


To be or not to be. It's a question.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So if BufferedReader adds new methods and doesn't change the existing methods, is it really a Decorator?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
Originally posted by Stan James:
So if BufferedReader adds new methods and doesn't change the existing methods, is it really a Decorator?

According to the structure of Decorator in GOF book, I think it's not.

But... I find it's still useful to add new methods.

:roll:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'd say it's a Decorator (as it adds functionality to the read() method, the buffering) - but the readline method is not part of the pattern.

If you like, you could probably say that it's not a "pure Decorator". Personally, I don't care...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Me, too. What's the worst thing that happens if we get the wrong answer here?
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
Originally posted by Stan James:
Me, too. What's the worst thing that happens if we get the wrong answer here?

Nothing.

I'm just learning the patterns one by one. So I want to know exactly what it is. Am I going in a wrong way?

Thanks.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Louis Wang:
Am I going in a wrong way?


Design Patterns aren't cookie cutters or Simplicity (sewing) patterns. There is no need for a 100% match of all features. Given a problem of a particular type it represents the essence of a recurring solution that balances often competing forces of design principles.

Given that the motivation and the "balance" for any concrete problem is going to be somewhat different from the ones stated in the pattern description it must be expected that every application of a pattern is going to emphasize some aspects over others, sometimes to the point that some non-essential (but often useful) aspects may be ignored. When you apply patterns you are not just working in black and white, you are very much working in continuum of grays, using what you need for your particular problem (which sometimes means not using patterns at all).

Ultimately mastery of the Object-Oriented design priciples is more important than rote learning of a litany of design patterns. Familiarity with these design principles will tremendously aid in the understanding of design patterns as each of them typically use a number of these principles to arrive at their essential solution. These design principles also guide you when you adapt design patterns to your concrete problems.

Also don't lose sight of the fact that design patterns greatest contribution is probably in the area of design communication (written or oral).


What Larry Wall said about Perl holds true: "When you say something in a small language, it comes out big. When say something in a big language, it comes out small". The same is true for English. The reason that biologist Ernst Haeckel could say "Ontogeny recapitulates phylogeny" in only three words was that he had these powerful words with highly specific meanings at his disposal. We allow inner complexity of language because it enables us to shift that complexity away from the individual utterance.

p. xlv, The Ruby Way, 2e (amazon US) by Hal Fulton

In other words design patterns let us express "big ideas" simply.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Although I agree with everything Peer said, to truly understand the gist of a pattern, it certainly seems to make sense to also look at it from black vs. white point of view. So, no, I don't think that you are going the wrong way.

As an aside, http://industriallogic.com/papers/learning.html might be interesting to you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Decorator pattern within java.io
 
Similar Threads
searching a string in an HTML file
BufferedWriter, PrinterWriter etc
java io question, conceptual query
List of Patterns used in Java SE
BufferedReader