A Classic Example of Decorator: Streams

The Java class libraries provide a classic example of the DECORATOR pattern in the overall design of Java input and output streams. A stream is a serial collection of bytes or characters, such as those that appear in a document. In Java, each stream object usually contains another stream object to which it forwards its operations. This structure is typical of DECORATOR.

The DECORATOR pattern arranges for each decorator object to contain another decorator object. In this regard, a decorator is like a slim composite whose elements each have a single child. Unlike the COMPOSITE pattern, whose purpose is to compose aggregate objects, the purpose of DECORATOR is to compose behavior.

Structurally, DECORATOR

Get Design Patterns Java™ Workbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.