11
DESIGN PATTERN READING GROUP – DECORATOR PATTERN 2014/May/30 Vincent Chu

Design pattern reading group – decorator pattern

Embed Size (px)

Citation preview

Page 1: Design pattern reading group – decorator pattern

DESIGN PATTERN READING GROUP –

DECORATOR PATTERN

2014/May/30Vincent Chu

Page 2: Design pattern reading group – decorator pattern

AGENDA

What is Decorator Pattern?

Examples

Conclusion

Page 3: Design pattern reading group – decorator pattern

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

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

EXAMPLES

Cup of Joe Beauty Salon

Page 6: Design pattern reading group – decorator pattern

EXAMPLE – COFFEE

Page 8: Design pattern reading group – decorator pattern

CONCLUSION

Consider following pattern before you choice Decorator Adapter Proxy Strategy Composite

Page 9: Design pattern reading group – decorator pattern

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

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

THANKS FOR YOUR TIME!