Download presentation
Presentation is loading. Please wait.
Published byNigel Thomas Modified over 5 years ago
1
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object is the component, it is the decorated object. The decorator conforms to the interface of the enclosed component and so its presence is transparent to the components clients. The decorator forwards requests to the component, but may perform some processing before/after doing so. Oct 2003 Ron McFadyen
2
:border would draw itself;
Decorator Pattern e.g. In a windowing environment, scrolling bars, borders, etc. could be decorators on top of the text view of a document. :border :scrollBar :textView component component draw() draw() draw() When it’s necessary for the document to appear (to draw itself), the draw message would be sent to :border and then: :border would draw itself; :border would send the draw message to :scrollBar which would draw itself; :scrollBar would send the draw message to :textView. Oct 2003 Ron McFadyen
3
Generic UML class diagram 1 Client Component
Decorator Pattern How would you characterize the components? Generic UML class diagram 1 Client Component Operation() Decorated Decorator Operation() Operation() other() Decorator1 Decorator2 … Decorator n Oct 2003 Ron McFadyen
4
Decorator Pattern - example
Consider a the NextGenPOS system. It must produce a sales receipt. A sales receipt will have a header and a footer, and perhaps … more than one header … more than one footer. Suppose we add coupons to the sales receipt … perhaps based on the products purchased / the season / information about the customer / etc. Time of day header Product2 coupon header Line item 1 Line item 2 Line item 3 … Money saved footer Oct 2003 Ron McFadyen
5
Decorator Pattern - example
UML class diagram 1 sale DecoratedReceipt print() All classes, in this application of the decorator pattern, implement the print method receipt Decorator print() print() other() timeOfDay productCoupon moneySaved Oct 2003 Ron McFadyen
6
Decorator Pattern – example object diagram
a sale object is related to a receipt, but the receipt is decorated with headers and footers (as this particular receipt requires) s:sale h1: timeOfDay h2:productCoupon decorators f1: moneySaved r: receipt Oct 2003 Ron McFadyen
7
Decorator Pattern - example Printing the receipt
s:sale h1: timeOfDay h2:productCoupon f1: moneySaved r: receipt print() print() printTime() print() printCoupon() print() print() printNote() Oct 2003 Ron McFadyen
8
Exercise Suppose we have simple stream files and various actions that can be applied to them sequentially: encrypt, decrypt, compress, decompress, … A file is encrypted and compressed :compress :encrypt :aFile A file is decompressed and decrypted :decrypt :decompress :bFile Oct 2003 Ron McFadyen
9
Decorator Pattern - exercise
UML class diagram 1 wordProcessor DecoratedFile write() All classes, in this application of decorator pattern, implement the write method file Decorator write() write() other() decrypt encrypt compress decompress write decrypt write encrypt write compress write decompress Oct 2003 Ron McFadyen
10
Decorator Pattern - exercise Saving the file
:wordProcessor :compress :encrypt :aFile save() write() compress() write() encrypt() write() Oct 2003 Ron McFadyen
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.