DarthVader DarthVader - 1 year ago 173
Java Question

Decorator Pattern for IO

I have read in wikipedia that Decorator pattern is used for .Net and Java IO classes.

Can someone explain how this is being used? and what s the benefit of it with a possible example?

There is an example of window forms on wiki but i wanted to know how it happens with IO classes.

Answer Source

InputStream is an abstract class. Most concrete implementations like BufferedInputStream, GzipInputStream, ObjectInputStream, etc have a constructor which takes an instance of the same abstract class. That's the recognition key of the decorator pattern (this also applies to constructors taking an instance of the same interface).

When such a constructor is been used, then all methods will delegate to the wrapped instance, with here and there changes in the way how the methods behave. For example, buffering the stream in memory beforehand, or decompressing the stream beforehand, or interpreting the stream differently, etcetera. Some have even additional methods which finally also delegate further to the wrapped instance. Those methods decorate the wrapped instance with extra behaviour.

Let's say that we have a bunch of serialized Java objects in a Gzipped file and that we want to read them a bit quickly.

First open an inputstream of it:

FileInputStream fis = new FileInputStream("/objects.gz");

We want speeeed, so let's buffer it in memory:

BufferedInputStream bis = new BufferedInputStream(fis);

The file is gzipped, so we need to ungzip it:

GzipInputStream gis = new GzipInputStream(bis);

We need to unserialize those Java objects:

ObjectInputStream ois = new ObjectInputStream(gis);

Now we can finally use it:

SomeObject someObject = (SomeObject) ois.readObject();
// ...

The benefit is that you have pretty plenty of freedom to decorate the stream using one or more various decorators to suit your needs. That's much better than having a single class for every possible combination like ObjectGzipBufferedFileInputStream, ObjectBufferedFileInputStream, GzipBufferedFileInputStream, ObjectGzipFileInputStream, ObjectFileInputStream, GzipFileInputStream, BufferedFileInputStream, etc..etc..

Note that when you're about to close the stream, just closing the outermost decorator is sufficient. It will delegate the close call all the way to the bottom.


See also:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download