1 / 22

Decorator Pattern

Decorator Pattern. Nitant Patel CSI 668 Oct. 08, 2009. Real life example. Let’ say I wake up one morning of an interview and look in the mirror and say…. What should I do?. Should I find some one else to go to an interview? No. “oh Nitant, you do not look good.”.

raine
Download Presentation

Decorator Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decorator Pattern Nitant Patel CSI 668 Oct. 08, 2009

  2. Real life example Let’ say I wake up one morning of an interview and look in the mirror and say…

  3. What should I do? Should I find some one else to go to an interview? No. “oh Nitant, you do not look good.”

  4. I will take shower, wear nice formal shirt and pant, may wear a tie, suit etc… etc…

  5. In other words, we decorate the object with other objects. That is the kind of idea we will follow here.

  6. What is Decorator Pattern? A Structural design pattern which focuses on attaching new behaviour to an existing object dynamically.

  7. Usage Scenario - Adding new features to individual objects at run-time without effecting other objects. - When extension of program by sub-classing (adds behaviour at compile time) is not practical.

  8. Objective This pattern is designed so that multiple capabilities can be arranged on top of each other to form a larger structure.

  9. Structure Diagram Defines the interface for objects that can have responsibilities added to them dynamically Defines an object to which additional responsibilities can be attached.

  10. Example in Java - Adding Scroll bars to a GUI Controls in Java. Let us consider JTextArea Component - It does not have scroll bars. So to add a scroll bars to this component, we will use JScrollPane container.

  11. // JScrollPane decorates GUI components • JTextArea area = new JTextArea(20, 30); • JScrollPane scrollPane = new JScrollPane(area); • contentPane.add(scrollPane);

  12. Another Real life example Suppose you like to gift someone during X’mas time. - First you select the gift - Next step is to wrap the gift Now the gift can be wrapped in multiple ways.

  13. Gift-Paper Manufacturers They could provide various options for wrapping gift: • box wrapped with gift-paper • box wrapped with gift-paper-with-crepe-paper • box wrapped with gift-paper-with-bow-without-crepe-paper • box wrapped with gift-paper-with-bow and crepe-paper • box wrapped with gift-paper with bow and crepe-paper and card • box wrapped with gift-paper with bow and crepe-paper without card • box wrapped with gift-paper without bow with crepe-paper and card • box wrapped with gift-paper without bow with crepe-paper without card • And so on…………………………………………………………………………….

  14. Problem! You may get confused in selecting the right option for your gift.

  15. Solution In order to solve the above problem, the manufacturers sell the following materials: • Boxes • Gift Paper • Cards • Bows • Crepe-paper

  16. Solution (Conti…) You simply buy materials of your choice, quantity as needed. And you also use as much as you want and keep the unused materials for later use.

  17. Advantages • Dynamic addition of new features. • Keeps other objects unaffected. • Eliminates the need for sub-classing.

  18. Disadvantage • Rapid growth of run-time instances.

  19. Conclusion This pattern is useful when components are lightweight.

  20. References -UML Diagram: http://java-x.blogspot.com/2007/01/implementing-decorator-pattern-in-java.html -Picture of Decorating Cake: www.coordinatedsuccess.com -Lecture notes by Professor Robert Stehwien (unm.edu): http://www.cs.unm.edu/~rstehwien/CS580/lectures/Design%20Patterns.pdf -Other Tutorial http://www.programmersheaven.com/2/Tutorial-Design-Patterns-Structural-Decorator

  21. Q & A

  22. Thank you

More Related