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
Very nice explanation. Even though I read about this pattern many times, only today I understood it. Thank you so much sir.
Well explained. Sir you mentioned that it is a structural design pattern. What does it mean? What are the other types ?
creational, structural, and behavioral
Nice sir…
Very Good one sir. I really like your classic examples.😀😀😀
Really enjoyable example
Explained well sir with the best example suiting the concept 😀
Better way to learn and remember the concept..👌
Concepts wideopened and beautifully illustrated.
Nice example sir.you are the best in explaining a new concept with example.☺️
Excellent explanation with suitable example. Hat’s off you sir.
Classic example sir.
When are you taking the next class in Bangalore?
Classic example sir. When are you taking the class in Bangalore next?
Impressive!Gives a clear idea ………I am really glad to say it’s an interesting way of explanation to read . I learn new information from your blog, you are doing a great job . Keep it up 🙂
comprehensive explanation with apt example 🙂
Comprehensive explanation with apt example 🙂
Excellent way to understand Decorator design pattern……!!!
I thought it was an very difficult concept,but this example is very interesting and very helpful to remember and also it made the concept very easy.
I thought it was a very difficult concept,but this example is very interesting and very helpful to remember and also it made the concept very easy.
Now i can prepare any type of biryani using at run time 😁. Thanks to decorator design pattern.
Well explained sir, hats off.
Sir does this decorator design pattern have a particular format? Does Structural design pattern have a particular format?
Like in Singleton Design Pattern we have the private constructor, static instance creation and static method to access the static instance. And is all creational design patterns like Singleton Design Pattern?
Very well explained sir.
well explained sir
Example is more striking and whenever we think about the decorator pattern the taste of Biriyani will be remembered
Thank you sir. Now the concept become clear
Thank you sir. Well explained with apt example.
Thank you sir. Well explained with good example.