0
Definition
• The decorator pattern attaches additional
  functionality or responsibility to an object
  dynamically.
• It ...
Class Diagram
                       Component

                       methodA()
                       methodB()



Concr...
Key Points

Decorators have the same supertype as the object they are decorating.


Decorators can be applied at run time ...
Design Problem

Lets consider a coffee shop which serves Beverages to the
people coming to the shop.

People can ask for a...
Class Diagram For Coffee Shop
                                Beverage

                             getDescription()
    ...
Abstract Beverage Class/ Or Interface


public abstract class Beverage{
  String description = “Unknown Beverage”;
  publi...
Abstract Class CondimentDecorator
Public abstract class CondimentDecorator
  extends Beverage {
  public abstract String g...
Expresso Beverage
public class Espresso extends Beverage {
  public Espresso() {
      description = “Espresso”;
  }
  pub...
Condiments Class ( Mocha )
public class Mocha extend CondimentDecorator{
  Beverage bevarage;
  public Mocha(Beverage b)
 ...
Main Class Creating an Espresso, and a Double Mocha
                     Whip Dark Roast.

public class CoffeeShop{
  publ...
The Decorator Pattern
Upcoming SlideShare
Loading in...5
×

The Decorator Pattern

1,571

Published on

The Decorator Design Pattern

- Head First

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,571
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
50
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "The Decorator Pattern"

  1. 1. Definition • The decorator pattern attaches additional functionality or responsibility to an object dynamically. • It provides a flexible alternative to sub classing for extending functionality. Since in case we are sub classing we have to alter the code but with decorator. We can add the functionality with out modifying the original code.
  2. 2. Class Diagram Component methodA() methodB() Concrete Component Decorator methodA() methodA() methodB() methodB() Concrete Decorator Concrete Decorator Component Wrapped Component Wrapped methodA() methodA() methodB() methodB()
  3. 3. Key Points Decorators have the same supertype as the object they are decorating. Decorators can be applied at run time to any number of levels. The Decorator has the same supertype as the object it is decorating we either pass around a decorated object to the decorator or the object itself. The decorator adds its own behavior either before or after delegating to the object it decorated to do the rest of the job.
  4. 4. Design Problem Lets consider a coffee shop which serves Beverages to the people coming to the shop. People can ask for a normal espresso coffee, DarkRoast coffee, Espresso Coffee with Mocha and Milk and Soft Cream or be it any combination. We have to build a system which is “scalable”, “maintainable”, “easy to add new toppings”, “easy to alter the cost of coffee or toppings” etc etc..
  5. 5. Class Diagram For Coffee Shop Beverage getDescription() cost() HouseBlend Espresso DarkRoast CondimentDecorator getDescription(); Cost() Cost() Cost() Mocha Milk Beverage beverage Beverage beverage cost() cost() getDescription() getDescription()
  6. 6. Abstract Beverage Class/ Or Interface public abstract class Beverage{ String description = “Unknown Beverage”; public String getDescription() return description; public abstract double cost(); }
  7. 7. Abstract Class CondimentDecorator Public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); } We have added the abstract signature since we want every decorator to tell that it is added to the coffee.
  8. 8. Expresso Beverage public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() return 0.99; }
  9. 9. Condiments Class ( Mocha ) public class Mocha extend CondimentDecorator{ Beverage bevarage; public Mocha(Beverage b) {this.beverage = b;} public String getDescription() return beverage.getDescription() + “Mocha”; public double cost() return beverage.cost()+0.20; }
  10. 10. Main Class Creating an Espresso, and a Double Mocha Whip Dark Roast. public class CoffeeShop{ public static void main() { Beverage beverage = new Espresso(); Beverage bvg1 = new DarkRoast(); bvg1 = new Mocha(bvg1); bvg1= new Mocha(bvg1); bvg1 = new Whip(bvg1); System.out.println(“Final Cost”+ bvg1.cost()); }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×