Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.

Similar presentations


Presentation on theme: "Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called."— Presentation transcript:

1 Fall 2009ACS-3913 Ron McFadyen1 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.

2 Fall 2009ACS-3913 Ron McFadyen2 Decorator Pattern e.g. In a windowing environment, scrolling bars, borders, etc. could be decorators on top of the text view of a document. :textView:scrollBar:border component 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.

3 Fall 2009ACS-3913 Ron McFadyen3 Decorator Pattern Generic UML class diagram Component Operation() Decorated Operation() Decorator Operation() other() 1 Client Decorator1Decorator2Decorator n … How would you characterize the components?

4 Fall 2009ACS-3913 Ron McFadyen4 Decorator Pattern - example Consider 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. Line item 1 Line item 2 Line item 3 … Time of day header Product2 coupon header Money saved footer Note: appears last Note: appear first

5 Fall 2009ACS-3913 Ron McFadyen5 Decorator Pattern - example UML class diagram DecoratedReceipt print() receipt print() Decorator print() other() 1 sale timeOfDayproductCouponmoneySaved All classes, in this application of the decorator pattern, implement the print method

6 Fall 2009ACS-3913 Ron McFadyen6 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) h2:productCoupon h1: timeOfDays:sale r: receipt decorators f1: moneySaved

7 Fall 2009ACS-3913 Ron McFadyen7 Decorator Pattern - example Printing the receipt s:saleh1: timeOfDayh2:productCouponf1: moneySavedr: receipt print() printNote() printCoupon() printTime() print() All of h1, h2, f1, r can respond to print()


Download ppt "Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called."

Similar presentations


Ads by Google