SlideShare a Scribd company logo
The Decorator Pattern
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());
       }

More Related Content

PPT
Phani Kumar - Decorator Pattern
PPT
15 decorator pattern
PDF
Christmas shopping sales tags 3 power point slides and ppt diagram templates
PDF
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
PDF
Composite Pattern
PPT
Composite pattern
PDF
Decorator design pattern (A Gift Wrapper)
PPT
Composite Design Pattern
Phani Kumar - Decorator Pattern
15 decorator pattern
Christmas shopping sales tags 3 power point slides and ppt diagram templates
What’s New For SQL Optimization In DB2 9 And DB2 10 For z/OS
Composite Pattern
Composite pattern
Decorator design pattern (A Gift Wrapper)
Composite Design Pattern

Recently uploaded (20)

PDF
Cours de Système d'information about ERP.pdf
PPT
Lecture 3344;;,,(,(((((((((((((((((((((((
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PPTX
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PDF
Reconciliation AND MEMORANDUM RECONCILATION
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
DOCX
Business Management - unit 1 and 2
PPT
Chapter four Project-Preparation material
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PPTX
Lecture (1)-Introduction.pptx business communication
PDF
COST SHEET- Tender and Quotation unit 2.pdf
PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
Daniels 2024 Inclusive, Sustainable Development
PDF
IFRS Notes in your pocket for study all the time
PDF
Nidhal Samdaie CV - International Business Consultant
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Cours de Système d'information about ERP.pdf
Lecture 3344;;,,(,(((((((((((((((((((((((
unit 1 COST ACCOUNTING AND COST SHEET
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Belch_12e_PPT_Ch18_Accessible_university.pptx
Reconciliation AND MEMORANDUM RECONCILATION
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Business Management - unit 1 and 2
Chapter four Project-Preparation material
2025 Product Deck V1.0.pptxCATALOGTCLCIA
Lecture (1)-Introduction.pptx business communication
COST SHEET- Tender and Quotation unit 2.pdf
Deliverable file - Regulatory guideline analysis.pdf
Daniels 2024 Inclusive, Sustainable Development
IFRS Notes in your pocket for study all the time
Nidhal Samdaie CV - International Business Consultant
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Roadmap Map-digital Banking feature MB,IB,AB
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Ad
Ad

The Decorator Pattern

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