1. Structural Patterns
The Decorator Pattern
Design Patterns
Chapter 8C
Dec 21, 2012 Design Patterns Chapter 8C 1
2. Objectives
In this lecture, we will
• Introduce the Decorator pattern
• Discuss examples that take advantage of the Decorator
pattern
• Compare the decorator pattern with some other structural
patterns
Dec 21, 2012 Design Patterns Chapter 8C 2
3. Structural Patterns
• Structural Patterns are used when it is necessary to build
larger structures that are composed of other existing
classes and objects
– Re-use of existing implementations
• Structural class patterns use inheritance
• Structural object patterns use aggregation
• This lecture introduces the decorator pattern
– Classified as object structural
Dec 21, 2012 Design Patterns Chapter 8C 3
4. Introduction to the Decorator Pattern
• The decorator pattern allows additional functionality to be
attached to an object dynamically
– A collection of books exist in a library
– How do you make some of the books available for loan?
• A graphical user interface toolkit is the most common
example of a motivation for using the decorator pattern
• Consider a user interface component such as view of a
document
– A page of text
• The view can be enhanced by adding scrolling or borders
– How can these be added?
Dec 21, 2012 Design Patterns Chapter 8C 4
5. Adding to a View
• Decorating a view
This is some text
that goes on and
on and on and on
and on and on and
on and on and on
and on and on and
Dec 21, 2012 Design Patterns Chapter 8C 5
6. Inheritance
• One way to add to a class is through inheritance
• A view with a border IS A view
• A view with a vertical scrollbar IS A view
• What does the class hierarchy for view look like?
Dec 21, 2012 Design Patterns Chapter 8C 6
7. Inheritance -Disadvantages
• Consider the classes on the previous slide
– There is a little bit of a class explosion
• Suppose another new type of enhancement to a view,
such as a 3D border, is required
– How many new classes are added to the hierarchy?
• A view is just one example of a GUI component
• There is a good chance that these enhancements may be
needed for other components
– How many more classes are needed to add borders to other
components?
Dec 21, 2012 Design Patterns Chapter 8C 7
8. Inheritance -Disadvantages
• When is it necessary to decide that a component has a
border?
• The choice has to be made statically
– A specific class has to be instantiated to achieve the desired
behaviour
– For example it is not possible to start with a view and then
add scrolling and then add a border
• A client cannot control how and when these decorations
are added to the component
• In this scenario the decorator pattern provides a more
flexible approach than inheritance
Dec 21, 2012 Design Patterns Chapter 8C 8
10. The Decorator Pattern
• Intent
– Attach additional responsibilities to an object dynamically
– The decorator pattern provides a flexible alternative to
inheritance for extending functionality
• Applicability
– The decorator pattern is used:
• To add responsibilities to individual objects dynamically and
transparently without affecting other objects
• For responsibilities that can be withdrawn
• When sub classing results in a class explosion
• When a class definition is hidden or otherwise unavailable for
subclassing
Dec 21, 2012 Design Patterns Chapter 8C 10
11. The Decorator Pattern
• Participants
• Component
– Defines the interface for objects that need to have
responsibilities added to them dynamically
• ConcreteComponent
– Defines an object that implements the component interface
and possibly adds additional responsibilities
• Decorator
– Defines an interface that conforms to the component
interface
– Maintains a reference to a component object
• ConcreteDecorator
– Adds respnsibilities to the component
Dec 21, 2012 Design Patterns Chapter 8C 11
13. Consequences
• The decorator pattern has at least two key advantages and two
liabilities:
• More flexible than static inheritance
– Responsibilities can be added and removed at run time
– The decorator pattern allows a property to be added more than
once; for example adding two borders (difficult to achieve through
inheritance)
• Avoids classes with excessive numbers of features high up in
the class hierarchy
– A pay as you go approach to adding responsibilities
• A decorator and its component are not identical
– Transparent enclosure but a decorated component is not identical
to the component itself
• Decorator pattern often results in lots of little objects that look
alike
Dec 21, 2012 Design Patterns Chapter 8C 13
14. The Library Example
• Consider building a library application
• Initially it is decided that all the library items are for
reference only
– That is they cannot be borrowed
• Once the system is up and running it is decided that some
of the library items can be borrowed
• How would you apply the decorator pattern in this
scenario?
Dec 21, 2012 Design Patterns Chapter 8C 14
15. Related Patterns
• A decorator is different from an adapter in that a decorator
only changes the responsibilities of an object not the
interface
– An adapter gives an object a new interface
• A decorator can be viewed as a degenerate composite
with only one component
– BUT a decorator adds responsibilities; it is not intended for
object aggregation
Dec 21, 2012 Design Patterns Chapter 8C 15
16. Summary
In this lecture we have:
• Introduced the Decorator pattern
• Discussed examples that take advantage of the Decorator
pattern
• Compared the decorator pattern with some other
structural patterns
Dec 21, 2012 Design Patterns Chapter 8C 16
Notes de l'éditeur
Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.