Decorator Design Pattern
Ahmad Alkhous
Damascus University
alkhousahmad@gmail.com
decorator pattern is a design pattern that allows behavior to be added
to an individual object, dynamically, without affecting the behavior of
other objects from the same class
What is Decorator design pattern?
What problems can the Decorator design
pattern solve?
• Responsibilities should be added to (and removed from) an object
dynamically at run-time.
• A flexible alternative to subclassing for extending functionality should
be provided.
What is the problem with subclassing ?
• With number of various functionalities that can be mixed together
Here comes the BIG problem
Who is hungry?
Beef Slice
Bread
Lettuce Cheddar Egg Mayo Ketchup
How to calculate the price of the order
Beef Slice
Bread
Lettuce Cheddar Egg Mayo Ketchup
5
10 3 2 5 1 1
How to calculate the price of the order
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
class BeefMeal:Icalculate{
..
..
public double calculate() {
return 10;
}
..
}
How to calculate the price of the order
interface Icalculate {
public double calculate();
}
class BeefMeal:Icalculate{
..
..
public double calculate() {
return 10;
}
..
}
class BeefMealWithLettuce : Icalculate
{
..
..
public double calulate()
{
return 10 +3 ;
}
}
Decorator UML Diagram
Our Burger Meal revisited
Demo
So Where is the Trick ?
So Where is the Trick ?
So Where is the Trick ?
• This allows multiple decorators to be
stacked on top of each other, each time
adding a new functionality to the overridden
method(s)
Another Famous Example – Java IO
//First open an inputstream of it:
FileInputStream fis = new FileInputStream("/objects.gz");
//We want speed, so let's buffer it in memory:
BufferedInputStream bis = new BufferedInputStream(fis);
//The file is gzipped, so we need to ungzip it:
GzipInputStream gis = new GzipInputStream(bis);
//We need to unserialize those Java objects:
ObjectInputStream ois = new ObjectInputStream(gis);
//Now we can finally use it:
SomeObject someObject = (SomeObject)ois.readObject();
Thank You

Decorator design pattern

  • 1.
    Decorator Design Pattern AhmadAlkhous Damascus University alkhousahmad@gmail.com
  • 2.
    decorator pattern isa design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class What is Decorator design pattern?
  • 3.
    What problems canthe Decorator design pattern solve? • Responsibilities should be added to (and removed from) an object dynamically at run-time. • A flexible alternative to subclassing for extending functionality should be provided.
  • 4.
    What is theproblem with subclassing ? • With number of various functionalities that can be mixed together Here comes the BIG problem
  • 5.
    Who is hungry? BeefSlice Bread Lettuce Cheddar Egg Mayo Ketchup
  • 6.
    How to calculatethe price of the order Beef Slice Bread Lettuce Cheddar Egg Mayo Ketchup 5 10 3 2 5 1 1
  • 7.
    How to calculatethe price of the order
  • 8.
    How to calculatethe price of the order interface Icalculate { public double calculate(); }
  • 9.
    How to calculatethe price of the order interface Icalculate { public double calculate(); } class BeefMeal:Icalculate{ .. .. public double calculate() { return 10; } .. }
  • 10.
    How to calculatethe price of the order interface Icalculate { public double calculate(); } class BeefMeal:Icalculate{ .. .. public double calculate() { return 10; } .. } class BeefMealWithLettuce : Icalculate { .. .. public double calulate() { return 10 +3 ; } }
  • 11.
  • 12.
    Our Burger Mealrevisited Demo
  • 13.
    So Where isthe Trick ?
  • 14.
    So Where isthe Trick ?
  • 15.
    So Where isthe Trick ? • This allows multiple decorators to be stacked on top of each other, each time adding a new functionality to the overridden method(s)
  • 16.
    Another Famous Example– Java IO //First open an inputstream of it: FileInputStream fis = new FileInputStream("/objects.gz"); //We want speed, so let's buffer it in memory: BufferedInputStream bis = new BufferedInputStream(fis); //The file is gzipped, so we need to ungzip it: GzipInputStream gis = new GzipInputStream(bis); //We need to unserialize those Java objects: ObjectInputStream ois = new ObjectInputStream(gis); //Now we can finally use it: SomeObject someObject = (SomeObject)ois.readObject();
  • 17.