Decorator pattern

415 views

Published on

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
415
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Decorator pattern

    1. 1. THE DECORATOR PATTERN
    2. 2. THE DECORATOR PATTERN CS362 Group One
    3. 3. THE PROBLEM
    4. 4. THE PROBLEMHow do you extend an existing object’s functionalitywithout having to create a subclass for each combinationof features?
    5. 5. FOR EXAMPLE
    6. 6. FOR EXAMPLEGiven a GUI window object, we want to add any of thefollowing features to the window: Horizontal scroll bar Vertical scroll bar BorderHow can one display a window object such that it is ableto display any combination of these features withoutwriting a class for each individual combination?
    7. 7. DECORATOR PATTERN
    8. 8. DECORATOR PATTERNExtends (decorates) the functionality of a certain objectat run-timeDecorating takes place independently of other instancesof the same classThe decorator class wraps around the original class orobject that is to be decorated
    9. 9. DECORATOR DIAGRAM
    10. 10. DECORATOR DIAGRAM
    11. 11. DECORATOR DIAGRAM
    12. 12. CODE—THE WINDOW // the Window interfaceinterface Window { public void draw(); // draws the Window}// implementation of a simple Window without any scrollbarsclass SimpleWindow implements Window { public void draw() { // draw simple window }}
    13. 13. CODE—THE DECORATOR // abstract decorator class - note that it implements Windowabstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } public void draw() { decoratedWindow.draw(); }}
    14. 14. CODE—THE DECORATOR // the first concrete decorator which adds vertical scrollbar functionalityclass VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { decoratedWindow.draw(); drawVerticalScrollBar(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar }}
    15. 15. CODE—THE DECORATOR // the second concrete decorator which adds horizontal scrollbar functionalityclass HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { decoratedWindow.draw(); drawHorizontalScrollBar(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar }}
    16. 16. CODE—THE MAIN public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); }}
    17. 17. QUESTIONS?

    ×