Decorator Design Pattern

Henry went to the restaurant and ordered one chicken biriyani. After 5 minutes the waiter brought the biriyani. Once when started eating Henry noticed that there is an egg in the biriyani along with two chicken pieces. Thinking that the waiter would have put that egg by mistake Henry called him and told, “I ordered chicken biriyani, but you kept an egg in that by mistake. Please take the egg back.” The waiter replied “The egg is free along with the chicken biriyani”.

Then Henry asked him “There are two chicken pieces, but why only one egg.?” 😀😀

 

An object which is created functions the way it is designed. But it is possible to modify the functionality of an object at run time. Thanks to Decorator design pattern. It is a structural pattern and is a wrapper to an existing class.

Hope you remember this code which you have done while learning Java I/O:

FileInputStream fis = new FileInputStream(“D:\\jThread\\jt.txt”);
BufferedInputStream bis = new BufferedInputStream(fis);

 

Let’s jump into an example, an example which most of my readers like it. We are going to make biriyani for all the people those who are reading this. I’m sure some of you are pure vegetarian, some eat egg but chicken and others eat both egg and chicken.

So here I call Biriyani as a component(An interface in this example) which has a method called prepareBiriyani() and OrdinaryBiriyani as a concrete component.

To decorate the biriyani I use a decorator called BiriyaniDecorator.  EggBiriyani and ChickenBiriyani are concrete decorators.

Now see the code for the same :

public interface Biriyani {

public String prepareBiriyani();

}

 

 

public class OrdinaryBiriyani implements Biriyani{

 

@Override

public String prepareBiriyani() {

return “Ordinary Biriyani”;

}

}

 

 

public abstract class BiriyaniDecorator implements Biriyani{

protected Biriyani biriyani;

 

public BiriyaniDecorator(Biriyani biriyani) {

this.biriyani = biriyani;

}

 

@Override

public String prepareBiriyani() {

return biriyani.prepareBiriyani();

}

}

 

public class EggBiriyani extends BiriyaniDecorator{

   public EggBiriyani(Biriyani biriyani) {
     super(biriyani);
}

@Override
   public String prepareBiriyani() {
       return biriyani.prepareBiriyani()+addEgg();

}

    private String addEgg(){
       return ” :: Egg “;
}

}

 

public class ChickenBiriyani extends BiriyaniDecorator{

public ChickenBiriyani(Biriyani biriyani) {

super(biriyani);

}

 

@Override

public String prepareBiriyani() {

return biriyani.prepareBiriyani()+addChicken();

}

 

private String addChicken(){

return ” :: Chicken “;

}

}

 

All major coding is done. Now let’s prepare and start serving:

public class TestBiriyani {

 

public static void main(String[] args) {

 

//Biriyani for vegetarians

Biriyani vegBiriyani = new OrdinaryBiriyani();

System.out.println(vegBiriyani.prepareBiriyani()); // It prints Ordinary Biriyani

 

//Biriyani for people who takes egg but chicken

Biriyani eggBiriyani = new EggBiriyani(new OrdinaryBiriyani());

System.out.println(eggBiriyani.prepareBiriyani());  // It prints Ordinary Biriyani :: Egg

 

//Biriyani for people who takes both egg and chicken

Biriyani chickenBiriyani = new ChickenBiriyani(new EggBiriyani(new OrdinaryBiriyani()));

System.out.println(chickenBiriyani.prepareBiriyani()); // It prints Ordinary Biriyani :: Egg :: Chicken

}

}

 

 

Saju Pappachen

 

About the author: Saju Pappachen is the Chief Consultant of jThread IT Training & Consultancy. He has more than 20 years of experience in Java technology.

He can be reached at saju@jthread.com

27 thoughts on “Decorator Design Pattern

Leave a Reply

Your email address will not be published. Required fields are marked *