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 classBasicCoffee implements Coffee{ public StringmakeCoffee ( ) { 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 StringmakeCoffee ( ) { returncoffee . makeCoffee( );} }
MilkCoffee.java
package com.app.design.decorator; public classMilkCoffee extends CoffeeDecorator{ publicMilkCoffee ( Coffee coffee){ super( coffee);} public StringmakeCoffee ( ) { returncoffee . makeCoffee ( ) + " Milk";} }
EggCoffee.java
package com.app.design.decorator; public classEggCoffee extends CoffeeDecorator{ publicEggCoffee ( Coffee coffee){ super( coffee);} public StringmakeCoffee ( ) { returncoffee . makeCoffee ( ) + " Egg";} }
Client.java
com.app.design.decorator; public class Client package { public static void main( String[ ] args) { Coffee coffee = newBasicCoffee ( ); System. out. println ( coffee. makeCoffee ( )); CoffeemilkCoffee = newMilkCoffee ( newBasicCoffee ( )); System. out. println ( milkCoffee . makeCoffee ( )); CoffeeeggCoffee = newEggCoffee ( newBasicCoffee ( )); System. out. println ( eggCoffee . makeCoffee ( )); CoffeemilkEggCoffee = newEggCoffee ( newMilkCoffee ( newBasicCoffee ( ))); 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
Reviewed by Ravi Yasas
on
10:34 AM
Rating:

Hi there! I know this is kinda off topic but I'd figured I'd ask.
ReplyDeleteWould 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!