27 Decorator

TO EXTEND A CODEBASE, you ordinarily add new classes or methods to it. Sometimes, though, you want to compose an object with new behavior at runtime. The INTERPRETER pattern, for example, lets you compose an executable object whose behavior changes radically, depending on how you compose it. In some cases, you may need small variations in behavior and want to be able to mix them together. The DECORATOR pattern addresses this need.

The intent of DECORATOR is to let you compose new variations of an operation at runtime.

A Classic Example: Streams and Writers

The Java class libraries provide a classic example of the DECORATOR pattern in the overall design of input and output streams. A stream is a serial collection of ...

Get Design Patterns in Java™, Second Edition now with the O’Reilly learning platform.

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