Summary

When you have an object type that needs a variety of behaviors that you might compose in various ways, the DECORATOR pattern offers a flexible alternative to creating a class for every possible behavioral combination. The Java class libraries provide a classic example of DECORATOR in the implementation of input and output streams. In application code, you can apply DECORATOR to set up function hierarchies. This lets you create a large family of function objects from a fixed set of function classes.

It is easy to mistake examples of “decoration” in a design as instances of DECORATOR. A design may allow runtime customization of an object's attributes and its behavior without using DECORATOR. The intent of DECORATOR is to let you compose ...

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.