• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Phani Kumar - Decorator Pattern

Phani Kumar - Decorator Pattern



Phani Kumar - Decorator Pattern

Phani Kumar - Decorator Pattern



Total Views
Views on SlideShare
Embed Views



2 Embeds 37

http://melbournepatterns.org 32
http://www.slideshare.net 5



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Phani Kumar - Decorator Pattern Phani Kumar - Decorator Pattern Presentation Transcript

    • Decorator Pattern By Phani
      • Intent
      • Motivation
      • Decorator Implementation
      • Disadvantages
    • The intent of The Decorator pattern referring to GoF book "Attach additional responsibilities to an object dynamically. Decorators provide a flexible alterative to subclassing for extending functionality." 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
    • Motivation
    • Example – Coffee
    • Example – Coffee How the design would look by adding several condiments like steamed milk, chocolate and mocha
    • Example – Coffee Modifying above design to avoid class explosion
    • Disadvantages of above design
      • Based on the assumption that condiments are included as part of the cost of coffee.
        • Price changes to condiments will force a change to all classes.
      • Additional condiments (like sugar) will force code changes into all existing classes.
      • All beverages will inherit all condiments regardless if required due to the inheritance.
      • What if customer needs multiple sugars or double the chocolate?
    • 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.
    • Meet The Decorator cost() Constructing the above example with the Decorator pattern. Espresso cost() Espresso Mocha cost()
    • Meet The Decorator cost() cost() Espresso Mocha Chocolate cost()
    • UML for Decorator
    • What we know so far…
      • Decorators have the same subtype as the objects they decorate.
        • - public class Espresso extends Beverage
        • - public abstract class CondimentDecorator extends Beverage
      • You can use one or more decorators (such as a variety of condiments) to wrap an object.
      • 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.
        • - Beverage espresso = new DarkRoast();
        • - Beverage mocha = new SteamedMilk(espresso);
        • - Beverage chocolate = new Chocolate(mocha);
      • Objects can be decorated at anytime, so we can decorate objects dynamically at runtime with as many decorators as we like. Such as:
        • public double cost() {
        • return beverage.cost() + 1.5;
        • }
    • Coffee Design with Decorator
    • Real World Decorators: Java I/O
    • Disadvantages of Decorator
      • 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.
      • 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.