OMG! Patterns.Decorator                       By Max Titov                        maxtitov.me          Ninja Software Oper...
OMG! Patterns?▶   About mostly used patterns▶   Examples of usage in real life▶   Learning by code examples▶   Learning ne...
ExampleBeauty salon.                  Salon businessSalon provides following services:  ▶   Haircut  ▶   Coloring  ▶   Sty...
The Objective▶   Salon going to add list of new services▶   Salon want to have different combinations    of services in th...
System Overviewclass Salon{   string HaircutDescription();   string StylingDescription();   string ColoringDescription();}
Don’t you feel like?    WHY?
Identifying the problems▶   There is only one thing wrong with this    approach: everything.    ▶   God class. Single resp...
Lets do a bit of refactoring▶   Identify common interface (if any)▶   Apply single responsibility principle▶   Use of old ...
System Overviewinterface ISalonService{   string getDescription();}
System Overviewclass Haircut: ISalonService{   string getDescription()   {       return ‘Haircut’;   }}
System Overview
Muuuuuch better!
Adding new functionality
Requirements updateClient: Hey, we want to add twoother services and remove one.That wouldn’t take much timeright?
Requirements update                             F@#kClient: Right?
Identifying the problems▶   Requires new class for each combination    of services▶   Hard to extend and maintain
Decorator▶   The Decorator Pattern attaches additional    responsibilities to an object dynamically.    Decorators provide...
Decorator UML
Abstract Decoratorclass abstract SalonServiceDecorator:ISalonService{   SalonServiceDecorator (ISalonService service);   s...
Concrete Decoratorclass HaircutDecorator{   HaircutDecorator (ISalonService service);   string getDescription();}
Use of DecoratorssalonService = new SalonService();salonService = newHaircutDecorator(salonService)salonService = newStyli...
System Overview
Success!
Code Time!
Where Used▶   GUI Frameworks       BaseElement          ScrollbarBehavior(BaseElement)          ShadowBehavior(BaseElement...
Pros and ConsPros:+ The cure for ugly code+ Composition over inheritance (Adds flexibility)+ Add behavior at runtime witho...
Books▶   Head First Design PatternsBy Eric Freeman▶Design Patterns : Elements ofReusable Object-Oriented SoftwareBy (GoF) ...
What next?More patterns!      
Questions?  OMG! Patterns. Decorator          By Max TitovGet examples: www.maxtitov.me   https://github.com/eolexeGet in ...
Omg! Patterns <decorator> by Max Titov
Upcoming SlideShare
Loading in …5
×

Omg! Patterns <decorator> by Max Titov

325
-1

Published on

Learn Design Patterns fun by code.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
325
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Omg! Patterns <decorator> by Max Titov

  1. 1. OMG! Patterns.Decorator By Max Titov maxtitov.me Ninja Software Operations
  2. 2. OMG! Patterns?▶ About mostly used patterns▶ Examples of usage in real life▶ Learning by code examples▶ Learning new language
  3. 3. ExampleBeauty salon. Salon businessSalon provides following services: ▶ Haircut ▶ Coloring ▶ Styling
  4. 4. The Objective▶ Salon going to add list of new services▶ Salon want to have different combinations of services in their pricelist▶ Salon want’s to expand for new market and provide services for males
  5. 5. System Overviewclass Salon{ string HaircutDescription(); string StylingDescription(); string ColoringDescription();}
  6. 6. Don’t you feel like? WHY?
  7. 7. Identifying the problems▶ There is only one thing wrong with this approach: everything. ▶ God class. Single responsibility principle? Never heard about… ▶ Code duplication ▶ Hard to test ▶ #@#$&%%!!!
  8. 8. Lets do a bit of refactoring▶ Identify common interface (if any)▶ Apply single responsibility principle▶ Use of old good inheritance
  9. 9. System Overviewinterface ISalonService{ string getDescription();}
  10. 10. System Overviewclass Haircut: ISalonService{ string getDescription() { return ‘Haircut’; }}
  11. 11. System Overview
  12. 12. Muuuuuch better!
  13. 13. Adding new functionality
  14. 14. Requirements updateClient: Hey, we want to add twoother services and remove one.That wouldn’t take much timeright?
  15. 15. Requirements update F@#kClient: Right?
  16. 16. Identifying the problems▶ Requires new class for each combination of services▶ Hard to extend and maintain
  17. 17. Decorator▶ The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality
  18. 18. Decorator UML
  19. 19. Abstract Decoratorclass abstract SalonServiceDecorator:ISalonService{ SalonServiceDecorator (ISalonService service); string getDescription();}
  20. 20. Concrete Decoratorclass HaircutDecorator{ HaircutDecorator (ISalonService service); string getDescription();}
  21. 21. Use of DecoratorssalonService = new SalonService();salonService = newHaircutDecorator(salonService)salonService = newStylingDecorator(salonService)salonService.getDescription()Output:> Salon service: Haircut, Styling
  22. 22. System Overview
  23. 23. Success!
  24. 24. Code Time!
  25. 25. Where Used▶ GUI Frameworks BaseElement ScrollbarBehavior(BaseElement) ShadowBehavior(BaseElement)▶ Streams FileStream GZipStream(ByteStream)
  26. 26. Pros and ConsPros:+ The cure for ugly code+ Composition over inheritance (Adds flexibility)+ Add behavior at runtime without class modification+ Behavior can be reusedCons:– Too many small classes– Could be not obvious for other developers(example: Java streams)
  27. 27. Books▶ Head First Design PatternsBy Eric Freeman▶Design Patterns : Elements ofReusable Object-Oriented SoftwareBy (GoF) Gamma, Helm, Johnson, Vlissides▶ Search Google for: Design patterns
  28. 28. What next?More patterns! 
  29. 29. Questions? OMG! Patterns. Decorator By Max TitovGet examples: www.maxtitov.me https://github.com/eolexeGet in touch: eolexe@gmail.com Twitter: eolexe

×