Java Reference
In-Depth Information
7
WHAT'S IN THIS CHAPTER?
How to implement the decorator pattern in plain code
How the decorator pattern solved a real‐life dilemma
How to implement the decorator pattern using the @Decorator
and @Delegate annotations
How to make the decorator pattern pluggable via the deployment
descriptors
How to use qualii ers to gain i ne‐grain control over how
decorators are used
WROX.COM CODE DOWNLOAD FOR THIS CHAPTER
The wrox.com code download for this chapter is found at www.wrox.com/go/
projavaeedesignpatterns o n the Download Code tab. The code is in the Chapter 07
download and individually named according to the names throughout the chapter.
The GoF 1 book describes the decorator pattern as “Attach additional responsibilities to an
object dynamically” and gives a Graphical User Interface Toolkit as an example. This is an
excellent real‐world example because adding new styles or behaviors to a user interface (UI)
toolkit is the perfect job for the decorator pattern.
The Head First Design Patterns 2 book gives a coffee shop as an example of different options
such as whip cream being added to the product. The addition of each new condiment wraps
the beverage object and adds new behavior to the description and price. This example has been
the best‐i t solution since the authors had a similar real‐life experience. See the next War Story.
The decorator pattern relies on component and decorator objects, which implement the same
interface. The decorator has an instance variable that implements the same interface so it can
 
Search WWH ::




Custom Search