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.

Chapter 15 • The Decoi<br />

decorated with any combination of behaviors. Some of the possibilities<br />

available for stream input are shown in Table 15-1.<br />

Developers in object-oriented languages can take advantage of this<br />

by having source and behavior objects derive from a common<br />

abstract class. Each behavior object can be given its source or prior<br />

behavior in its constructor. A chain of actions is then built as the<br />

objects themselves are instantiated (each is given a reference to its<br />

trailing object). The sources derive from ConcreteComponent (see<br />

Figure 15-4), while the behaviors are decorators. Note that<br />

Con-creteComponent is now a misnomer because it is now<br />

abstract.<br />

Table 15-1 Kinds of Sources and Behaviors<br />

Sources Behaviors<br />

String<br />

File<br />

Socket (TCP/IP)<br />

Serial port<br />

Parallel port<br />

Keyboard<br />

Buffered input<br />

Run checksum<br />

Unzip<br />

Decrypt (any number of ways)<br />

Selection filters (any number of ways)<br />

For example, to get the behavior "read from a file, decompress the<br />

input, and then decrypt it," do the following:<br />

1. Build the decorator chain by doing the following:<br />

a. Instantiate a file object.<br />

b. Pass a reference to it to the constructor of a decompression<br />

object.<br />

c. Pass a reference to that to the constructor of a decryption<br />

object.<br />

Languages reflect<br />

this

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

Saved successfully!

Ooh no, something went wrong!