Decorator Pattern 1. 10 ¢ Design: Which classes must be included? Story As a user I want a weekend…
Design pattern reading group – decorator pattern
-
Upload
vincent-chu -
Category
Technology
-
view
88 -
download
0
Transcript of Design pattern reading group – decorator pattern
![Page 1: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/1.jpg)
DESIGN PATTERN READING GROUP –
DECORATOR PATTERN
2014/May/30Vincent Chu
![Page 2: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/2.jpg)
AGENDA
What is Decorator Pattern?
Examples
Conclusion
![Page 3: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/3.jpg)
WHAT IS DECORATOR PATTERN?
Definition
In object-oriented programming, the decorator pattern (also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
UML
![Page 4: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/4.jpg)
WHAT IS DECORATOR PATTERN?Implementation
1. Subclass the original "Component" class into a "Decorator" class (see UML diagram);
2. In the Decorator class, add a Component pointer as a field;
3. Pass a Component to the Decorator constructor to initialize the Component pointer;
4. In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.
![Page 5: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/5.jpg)
EXAMPLES
Cup of Joe Beauty Salon
![Page 6: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/6.jpg)
EXAMPLE – COFFEE
![Page 7: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/7.jpg)
EXAMPLE – BEAUTY SALON
http://www.slideshare.net/eolexe/omg-patterns-ltdecorator-by-max-titov
Page 3
![Page 8: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/8.jpg)
CONCLUSION
Consider following pattern before you choice Decorator Adapter Proxy Strategy Composite
![Page 9: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/9.jpg)
CONCLUSION
Pros
1) The cure of ugly code
2) Composition over inheritance
3) Add behavior at runtime without class modification
4) Behavior can be reuse
Cons
1) Tons of small classes
2) Could be not obvious for other developers
![Page 10: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/10.jpg)
REFERENCE
Wikipedia - http://en.wikipedia.org/wiki/Decorator_pattern
Photo & Conclusion - http://www.slideshare.net/eprafulla/01-introduction-and-decorator-pattern
Beauty Salon - http://www.slideshare.net/eolexe/omg-patterns-ltdecorator-by-max-titov
Cup of Joe - http://www.slideshare.net/melbournepatterns/phani-kumar-decorator-pattern
Coffee - http://internshipblogg.wordpress.com/2012/06/13/decorator-pattern-power-of-extension/
![Page 11: Design pattern reading group – decorator pattern](https://reader036.fdocuments.us/reader036/viewer/2022082921/556191a8d8b42a91778b51ac/html5/thumbnails/11.jpg)
THANKS FOR YOUR TIME!