Decorator Design Pattern



This is another design pattern that can be used to decorate an existing object without touching the structure of it. This is a very useful design pattern.

Just a moment think about making a coffee. You can make a coffee and then you can add milk or egg as decorators for that existing coffee. It may give an additional taste of your coffee. The decorator design pattern gives this approach in a simple way.

The decorator design pattern shows how doing that thing in programming. Look at the following diagram to understand.




In this implementation, there are few components that are very important to implement the Decorator design pattern. I'm going to create a decorator for making a coffee.

  • Basic interface of coffee
  • Basic coffee
  • Coffee decorator
  • Milk coffee
  • Egg coffee
  • Client

Coffee.java

package com.app.design.decorator;

public interface Coffee {
    public String makeCoffee();
} 



BasicCoffee.java

package com.app.design.decorator;

public class BasicCoffee implements Coffee{
    public String makeCoffee(){
        return "Normal coffee";
    }
}



CoffeeDecorator.java

package com.app.design.decorator;

public abstract class CoffeeDecorator implements Coffee {

   protected Coffee coffee;
 
   public CoffeeDecorator(Coffee coffee){
       this.coffee = coffee;
   }
 
   @Override
   public String makeCoffee(){
       return coffee.makeCoffee();
   }
}



MilkCoffee.java

package com.app.design.decorator;

public class MilkCoffee extends CoffeeDecorator {
   public MilkCoffee(Coffee coffee) {
       super(coffee);
   }
 
   public String makeCoffee(){
       return coffee.makeCoffee() + " Milk";
   }
}



EggCoffee.java

package com.app.design.decorator;

public class EggCoffee extends CoffeeDecorator {
    public EggCoffee(Coffee coffee) {
       super(coffee);
    }
 
    public String makeCoffee(){
       return coffee.makeCoffee() + " Egg";
    }
}



Client.java

package com.app.design.decorator;

public class Client {
    public static void main(String[] args){
  
        Coffee coffee = new BasicCoffee();
        System.out.println(coffee.makeCoffee());
  
        Coffee milkCoffee = new MilkCoffee(new BasicCoffee());
        System.out.println(milkCoffee.makeCoffee());
  
        Coffee eggCoffee = new EggCoffee(new BasicCoffee());
        System.out.println(eggCoffee.makeCoffee());
  
        Coffee milkEggCoffee = new EggCoffee(new MilkCoffee(new BasicCoffee()));
        System.out.println(milkEggCoffee.makeCoffee());
    }
}




You may look at both the class diagram and these classes. When you run this program, you will be able to see it is very easy to decorate basic coffee with this implementation.

Advantages of Decorator class

  • It is easy to add new behaviors at run time without compiling the code.
  • Provide a flexible approach than inheritance when changing behaviors of objects.

Disadvantages of Decorator class

  • Create many objects
  • Complex to debug code (Because functions are added at run time)

When it uses in Java?

  • Decorator is used in the Java IO package
    • java.io.BufferedReader
    • java.io.FileReader
    • java.io.Reader

Decorator Design Pattern Decorator Design Pattern Reviewed by Ravi Yasas on 10:34 AM Rating: 5

1 comment:

  1. Hi there! I know this is kinda off topic but I'd figured I'd ask.
    Would you be interested iin exchanging links or maybe guest writing a blog post or vice-versa?
    My site discusses a lot of the same subjects as yours and I believe we
    could greeatly benefit from each other. If you happen to be interested feel free to shoot me an email.

    I look forward tto hearing from you! Awesome blog by the
    way!

    ReplyDelete

Powered by Blogger.