Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Omg! Patterns <decorator> by Max Titov


Published on

Learn Design Patterns fun by code.

  • Be the first to comment

  • Be the first to like this

Omg! Patterns <decorator> by Max Titov

  1. 1. OMG! Patterns.Decorator By Max Titov 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: in touch: Twitter: eolexe