Phani Kumar - Decorator Pattern


Published on

Phani Kumar - Decorator Pattern

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Phani Kumar - Decorator Pattern

  1. 1. Decorator Pattern By Phani
  2. 2. <ul><li>Intent </li></ul><ul><li>Motivation </li></ul><ul><li>Decorator Implementation </li></ul><ul><li>Disadvantages </li></ul>Agenda
  3. 3. The intent of The Decorator pattern referring to GoF book &quot;Attach additional responsibilities to an object dynamically. Decorators provide a flexible alterative to subclassing for extending functionality.&quot; Decorators are also known as “wrappers” , because a decorator wraps itself around another object. Today we will discuss why the Decorator design pattern provides a flexible alternative to subclassing as it demonstrates why subclassing can be dangerous. Intent
  4. 4. Motivation
  5. 5. Example – Coffee
  6. 6. Example – Coffee How the design would look by adding several condiments like steamed milk, chocolate and mocha
  7. 7. Example – Coffee Modifying above design to avoid class explosion
  8. 8. Disadvantages of above design <ul><li>Based on the assumption that condiments are included as part of the cost of coffee. </li></ul><ul><ul><li>Price changes to condiments will force a change to all classes. </li></ul></ul><ul><li>Additional condiments (like sugar) will force code changes into all existing classes. </li></ul><ul><li>All beverages will inherit all condiments regardless if required due to the inheritance. </li></ul><ul><li>What if customer needs multiple sugars or double the chocolate? </li></ul>
  9. 9. The Open – Closed Principle Design Principle : Classes should be open for extension, but closed for modification. The code should be designed to allow classes to be easily extended to incorporate new behavior (such as adding condiments) without modifying the existing code (such as Espresso). Therefore the design is resilient to change and flexible enough to take on new functionality to meet changing requirements.
  10. 10. Meet The Decorator cost() Constructing the above example with the Decorator pattern. Espresso cost() Espresso Mocha cost()
  11. 11. Meet The Decorator cost() cost() Espresso Mocha Chocolate cost()
  12. 12. UML for Decorator
  13. 13. What we know so far… <ul><li>Decorators have the same subtype as the objects they decorate. </li></ul><ul><ul><li>- public class Espresso extends Beverage </li></ul></ul><ul><ul><li>- public abstract class CondimentDecorator extends Beverage </li></ul></ul><ul><li>You can use one or more decorators (such as a variety of condiments) to wrap an object. </li></ul><ul><li>Given that the decorator has the same super type as the object it decorates, An enhanced decorated object can now be used in place of the original object. </li></ul><ul><ul><li>- Beverage espresso = new DarkRoast(); </li></ul></ul><ul><ul><li>- Beverage mocha = new SteamedMilk(espresso); </li></ul></ul><ul><ul><li>- Beverage chocolate = new Chocolate(mocha); </li></ul></ul><ul><li>Objects can be decorated at anytime, so we can decorate objects dynamically at runtime with as many decorators as we like. Such as: </li></ul><ul><ul><li>public double cost() { </li></ul></ul><ul><ul><li>return beverage.cost() + 1.5; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  14. 14. Coffee Design with Decorator
  15. 15. Real World Decorators: Java I/O
  16. 16. Disadvantages of Decorator <ul><li>Designs using this pattern often result in a larger number of smaller classes that can be overwhelming to a developer trying to use the Decorator-based API. </li></ul><ul><li>Introducing decorators can increase the complexity of code needed to instantiate the component. Once you’ve got decorators, you’ve got to not only instantiate the component, but also wrap it with who knows how many decorators. </li></ul>