SlideShare une entreprise Scribd logo
1  sur  16
Structural Patterns
               The Decorator Pattern

                    Design Patterns
                      Chapter 8C




Dec 21, 2012      Design Patterns Chapter 8C 1
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
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
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
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
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
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
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
Using the Decorator Pattern




Dec 21, 2012   Design Patterns Chapter 8C 9
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
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
The Decorator Pattern

 • Structure




Dec 21, 2012   Design Patterns Chapter 8C 12
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
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
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
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

Contenu connexe

En vedette

9. oo languages
9. oo languages9. oo languages
9. oo languagesAPU
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
6. activity diagrams
6. activity diagrams6. activity diagrams
6. activity diagramsAPU
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_moduleAPU
 
09 abstract classesandinterfaces
09 abstract classesandinterfaces09 abstract classesandinterfaces
09 abstract classesandinterfacesAPU
 
4.class diagramsusinguml
4.class diagramsusinguml4.class diagramsusinguml
4.class diagramsusingumlAPU
 
7. sequence and collaboration diagrams
7. sequence and collaboration diagrams7. sequence and collaboration diagrams
7. sequence and collaboration diagramsAPU
 
3. use cases
3. use cases3. use cases
3. use casesAPU
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation systemmuthumeenakshim
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagramsAPU
 
14 file handling
14 file handling14 file handling
14 file handlingAPU
 

En vedette (11)

9. oo languages
9. oo languages9. oo languages
9. oo languages
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
6. activity diagrams
6. activity diagrams6. activity diagrams
6. activity diagrams
 
01 introduction to_module
01 introduction to_module01 introduction to_module
01 introduction to_module
 
09 abstract classesandinterfaces
09 abstract classesandinterfaces09 abstract classesandinterfaces
09 abstract classesandinterfaces
 
4.class diagramsusinguml
4.class diagramsusinguml4.class diagramsusinguml
4.class diagramsusinguml
 
7. sequence and collaboration diagrams
7. sequence and collaboration diagrams7. sequence and collaboration diagrams
7. sequence and collaboration diagrams
 
3. use cases
3. use cases3. use cases
3. use cases
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation system
 
5.state diagrams
5.state diagrams5.state diagrams
5.state diagrams
 
14 file handling
14 file handling14 file handling
14 file handling
 

Similaire à Design Patterns Chapter 8C Decorator Pattern

Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
Design patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your ownDesign patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your ownCommit Software Sh.p.k.
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisYoung Alista
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisHoang Nguyen
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisFraboni Ec
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisTony Nguyen
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisJames Wong
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisHarry Potter
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisLuis Goldster
 
Design patterns Structural
Design patterns StructuralDesign patterns Structural
Design patterns StructuralUMAR ALI
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)APU
 

Similaire à Design Patterns Chapter 8C Decorator Pattern (20)

Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Design patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your ownDesign patterns: Understand the patterns and design your own
Design patterns: Understand the patterns and design your own
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
CBD.pptx
CBD.pptxCBD.pptx
CBD.pptx
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Design patterns Structural
Design patterns StructuralDesign patterns Structural
Design patterns Structural
 
Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)Design patterns structuralpatterns(theadapterpattern)
Design patterns structuralpatterns(theadapterpattern)
 
unit 3 Design 1
unit 3 Design 1unit 3 Design 1
unit 3 Design 1
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 

Plus de APU

. 1. introduction to object orientation
. 1. introduction to object orientation. 1. introduction to object orientation
. 1. introduction to object orientationAPU
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_moduleAPU
 
. 9. oo languages
. 9. oo languages. 9. oo languages
. 9. oo languagesAPU
 
. 8. design patterns
. 8. design patterns. 8. design patterns
. 8. design patternsAPU
 
. 5. state diagrams
. 5. state diagrams. 5. state diagrams
. 5. state diagramsAPU
 
. 4. class diagrams using uml
. 4. class diagrams using uml. 4. class diagrams using uml
. 4. class diagrams using umlAPU
 
. 2. introduction to uml
. 2. introduction to uml. 2. introduction to uml
. 2. introduction to umlAPU
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_moduleAPU
 
9.oo languages
9.oo languages9.oo languages
9.oo languagesAPU
 

Plus de APU (9)

. 1. introduction to object orientation
. 1. introduction to object orientation. 1. introduction to object orientation
. 1. introduction to object orientation
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_module
 
. 9. oo languages
. 9. oo languages. 9. oo languages
. 9. oo languages
 
. 8. design patterns
. 8. design patterns. 8. design patterns
. 8. design patterns
 
. 5. state diagrams
. 5. state diagrams. 5. state diagrams
. 5. state diagrams
 
. 4. class diagrams using uml
. 4. class diagrams using uml. 4. class diagrams using uml
. 4. class diagrams using uml
 
. 2. introduction to uml
. 2. introduction to uml. 2. introduction to uml
. 2. introduction to uml
 
. 01 introduction_to_module
. 01 introduction_to_module. 01 introduction_to_module
. 01 introduction_to_module
 
9.oo languages
9.oo languages9.oo languages
9.oo languages
 

Design Patterns Chapter 8C Decorator Pattern

  • 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
  • 9. Using the Decorator Pattern Dec 21, 2012 Design Patterns Chapter 8C 9
  • 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
  • 12. The Decorator Pattern • Structure Dec 21, 2012 Design Patterns Chapter 8C 12
  • 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

  1. 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.