Why use java.io.Filter* as opposed to extending a concrete stream implementation?
What is the purpose of the java.io.Filter* types? i.e. FilterInputStream
, FilterReader
Isn't it possible to add any filtering logic to a concrete stream class like FileInput开发者_如何学PythonStream
by simply overriding particular methods and making calls to super()
? i.e. super.read()
As far as I can tell, the only facility the filter types provide are perceptive at best -- that is, they document (at a glance) the fact that all the developer really needed was a way to apply some additional logic (filtering) to an already existing stream implementation.
What am I missing?
This is a Decorator Pattern, which then you can use to filter anything that extends an input stream. Meaning that you have high flexibility for it's use.
If you extend an input stream, then whenever you want to filter something you need to extend that type of input stream. So if you want to filter every type of input stream you'll need a filter extension class for every type of input stream. Using the decorator pattern you only need one implementation that accepts an Input Stream and you can filter them all.
精彩评论