0
Structural Patterns               The Decorator Pattern                    Design Patterns                      Chapter 8C...
Objectives In this lecture, we will • Introduce the Decorator pattern • Discuss examples that take advantage of the Decora...
Structural Patterns • Structural Patterns are used when it is necessary to build   larger structures that are composed of ...
Introduction to the Decorator Pattern • The decorator pattern allows additional functionality to be   attached to an objec...
Adding to a View • Decorating a view                        This is some text                        that goes on and     ...
Inheritance •   One way to add to a class is through inheritance •   A view with a border IS A view •   A view with a vert...
Inheritance -Disadvantages • Consider the classes on the previous slide     – There is a little bit of a class explosion •...
Inheritance -Disadvantages • When is it necessary to decide that a component has a   border? • The choice has to be made s...
Using the Decorator PatternDec 21, 2012   Design Patterns Chapter 8C 9
The Decorator Pattern • Intent     – Attach additional responsibilities to an object dynamically     – The decorator patte...
The Decorator Pattern • Participants • Component     – Defines the interface for objects that need to have       responsib...
The Decorator Pattern • StructureDec 21, 2012   Design Patterns Chapter 8C 12
Consequences • The decorator pattern has at least two key advantages and two   liabilities: • More flexible than static in...
The Library Example • Consider building a library application • Initially it is decided that all the library items are for...
Related Patterns • A decorator is different from an adapter in that a decorator   only changes the responsibilities of an ...
Summary In this lecture we have: • Introduced the Decorator pattern • Discussed examples that take advantage of the Decora...
Upcoming SlideShare
Loading in...5
×

Design patterns structuralpatterns(thedecoratorpattern)

203

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
203
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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.
  • Transcript of "Design patterns structuralpatterns(thedecoratorpattern)"

    1. 1. Structural Patterns The Decorator Pattern Design Patterns Chapter 8CDec 21, 2012 Design Patterns Chapter 8C 1
    2. 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 patternsDec 21, 2012 Design Patterns Chapter 8C 2
    3. 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 structuralDec 21, 2012 Design Patterns Chapter 8C 3
    4. 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. 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 andDec 21, 2012 Design Patterns Chapter 8C 5
    6. 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. 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. 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 inheritanceDec 21, 2012 Design Patterns Chapter 8C 8
    9. 9. Using the Decorator PatternDec 21, 2012 Design Patterns Chapter 8C 9
    10. 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 subclassingDec 21, 2012 Design Patterns Chapter 8C 10
    11. 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 componentDec 21, 2012 Design Patterns Chapter 8C 11
    12. 12. The Decorator Pattern • StructureDec 21, 2012 Design Patterns Chapter 8C 12
    13. 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 alikeDec 21, 2012 Design Patterns Chapter 8C 13
    14. 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. 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 aggregationDec 21, 2012 Design Patterns Chapter 8C 15
    16. 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 patternsDec 21, 2012 Design Patterns Chapter 8C 16
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×