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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.