7 Decorator Pattern

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 qualifiers to gain fine-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 on the Download Code tab. The code is in the Chapter 7 download and individually named according to the names throughout the chapter.

The GoF1 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 Patterns2 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-fit 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 ...

Get Professional Java EE 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.