Upload
vincent-chu
View
86
Download
0
Embed Size (px)
Citation preview
DESIGN PATTERN READING GROUP –
DECORATOR PATTERN
2014/May/30Vincent Chu
AGENDA
What is Decorator Pattern?
Examples
Conclusion
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
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.
EXAMPLES
Cup of Joe Beauty Salon
EXAMPLE – COFFEE
EXAMPLE – BEAUTY SALON
http://www.slideshare.net/eolexe/omg-patterns-ltdecorator-by-max-titov
Page 3
CONCLUSION
Consider following pattern before you choice Decorator Adapter Proxy Strategy Composite
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
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/
THANKS FOR YOUR TIME!