Java Reference
In-Depth Information
Figure 7-5.
Commonly used classes for input streams compared with the decorator pattern
Table 7-1.
Comparing the Class Design in the Decorator Pattern, the Drink Application, and Input Streams
Decorator Pattern
Drink Application
Input Stream
Component
Drink
InputStream
ConcreteComponentA
ConcreteComponentB
Rum
Vodka
Whisky
FileInputStream
ByteArrayInputStream
PipedInputStream
Decorator
DrinkDecorator
FilterInputStream
ConcreteDecoratorA
ConcreteDecoratorB
Honey
Spices
BufferedInputStream
PushbackInputStream
DataInputStream
ObjectInputStream
The
abstract
base component is the
InputStream
class, which is similar to the
Drink
class. You have concrete
component classes of
FileInputStream
,
ByteArrayInputStream
, and
PipedInputStream
, which are similar to the
Rum
,
Vodka,
and
Whiskey
classes. You have a
FilterInputStream
class, which is similar to the
DrinkDecorator
class. Notice the decorator class in the input stream family does not use the word “Decorator” in its class name; it
is named as
FilterInputStream
instead. It is also not declared
abstract
as you had declared the
DrinkDecorator
class. Not declaring it
abstract
seems to be an inconsistency in the class design. You have concrete decorator classes
of
BufferedInputStream
,
DataInputStream
, and
PushbackInputStream,
which are similar to the
Honey
and
Spices
classes in the drink application. One noticeable difference is that the
ObjectInputStream
class is a concrete decorator
and it is inherited from the
abstract
component
InputStream
, not from the
abstract
decorator
FilterInputStream
.
Note that the requirement for a concrete decorator is that it should have the
abstract
component class in its
immediate or non-immediate superclass and it should have a constructor that accepts an
abstract
component as its
argument. The
ObjectInputStream
class fulfills these requirements.