Chapter 8. Decorator Design Pattern
Charm, in most men and nearly all women, is a decoration.
This wasn’t just plain terrible, this was fancy terrible. This was terrible with raisins in it.
The ornament of a house is the friends who frequent it.
She well knew the great architectural secret of decorating her constructions, and never condescended to construct a decoration.
What Is the Decorator Pattern?
As a structural pattern, the Decorator adds to an existing structure. With the Adapter pattern, the addition to an existing structure is by adding an adapter class that resolves incompatible interfaces. The Decorator adds objects to existing objects. Also called wrapper (as was also the Adapter), the Decorator participant wraps the Component participant with concrete components. Figure 8-1 shows the class diagram, but before going over it, several details need to be considered.
First of all, the Decorator is one of the few design patterns that includes one abstract class inheriting another abstract class. As you can see in Figure 8-1, a looping aggregation connects the Decorator to the Component participant. (The looping line looks more like “wrapping” than the straight line used in the original Gang of Four’s class diagram—see also the Freemans’ Head First Design Patterns, page 91.) The Decorator can be said to “wrap” the Component.
The design creates both the component to be decorated and the decorators. Think of the Component ...
Get Learning PHP Design Patterns 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.