The Decorator Pattern
Upcoming SlideShare
Loading in...5
×
 

The Decorator Pattern

on

  • 1,807 views

The Decorator Design Pattern

The Decorator Design Pattern

- Head First

Statistics

Views

Total Views
1,807
Views on SlideShare
1,805
Embed Views
2

Actions

Likes
0
Downloads
41
Comments
0

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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.

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

The Decorator Pattern The Decorator Pattern Presentation Transcript

  • 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.
  • Class Diagram Component methodA() methodB() Concrete Component Decorator methodA() methodA() methodB() methodB() Concrete Decorator Concrete Decorator Component Wrapped Component Wrapped methodA() methodA() methodB() methodB()
  • 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.
  • 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..
  • 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()
  • Abstract Beverage Class/ Or Interface public abstract class Beverage{ String description = “Unknown Beverage”; public String getDescription() return description; public abstract double cost(); }
  • 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.
  • Expresso Beverage public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() return 0.99; }
  • 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; }
  • 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()); }