23.07.2012 Views

Design Patterns Explained

Design Patterns Explained

Design Patterns Explained

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

25O Part V • Handling Variations with <strong>Design</strong> <strong>Patterns</strong><br />

Instantiating the<br />

chains<br />

2. Read, decompress, and decrypt the data— all transparently to<br />

the client object that is using it. The client merely knows it has<br />

some sort of input stream object.<br />

If this client needs to get its input from a different source, the chain is<br />

created by instantiating a different source object using the same<br />

behavior objects.<br />

Understanding "The Complete Stream Zoo."*<br />

Java is notorious for a confusing array of stream inputs and associated<br />

classes. It is much easier to understand these classes in the<br />

context of the Decorator pattern. The classes directly derived<br />

from java.io.InputStream (ByteArrayInputStream,<br />

FileInputStream, FilterInputStream, InputStream,<br />

ObjectInputStream, SequenceInputStream, and<br />

String-BufferInputstream) all play the role of the decorated<br />

object. All of the decorators derive from the<br />

FilterlnputStream (either directly or indirectly).<br />

Keeping the Decorator pattern in mind explains why the Java<br />

language requires chaining these objects together when they are<br />

instantiated— this gives programmers the ability to pick any<br />

number of combinations from the different behaviors available.<br />

Horstmann, C., Core Java— Volume 1— Fundamentals, Palo Alto, CA: Pearson<br />

Education, 1999, p. 627.<br />

Field Notes: Using the Decorator Pattern<br />

The power of the Decorator pattern requires that the instantiation<br />

of the chains of objects be completely decoupled from the Client<br />

objects that use it. This is most typically accomplished through the<br />

use of factory objects that instantiate the chains based upon some<br />

configuration information.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!