In this post, We will talk and learn about the Decorator Design Pattern in Java.
- Decorator design pattern comes under structural design patterns.
- Decorator design pattern is used to modify the functionality of an object at runtime. At the same time, other instances of the same class will not be affected by this, so individual object gets the modified behavior.
- This Pattern provides a wrapper to the existing class.
- Decorator design pattern uses abstract classes or interfaces with the composition to implement the wrapper.
- The decorator design pattern is helpful in providing runtime modification abilities and hence more flexible. It’s easy to maintain and extend when the number of choices are more.
Decorator Design Pattern in JDK:
- Decorator pattern is used a lot in Java IO packages, such as FileReader, BufferedReader, BufferedInputStream etc.
- java.util.Collections ( synchronizedXXX() and unmodifiableXXX() methods)
- javax.servlet.http.HttpServletRequestWrapperand HttpServletResponseWrapper
Below is the complete source code:
Bike.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials.decorator; /** * @author KK JavaTutorials * Basic contract to assemble Bike */ public interface Bike { public abstract void assembleBike(); } |
BasicBike.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkjavatutorials.decorator.impl; import com.kkjavatutorials.decorator.Bike; /** * @author KK JavaTutorials * Assemble Bike with basic feature */ public class BasicBike implements Bike { @Override public void assembleBike() { System.out.println("Basic Bike.."); } } |
LuxuryBike.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials.decorator.impl; import com.kkjavatutorials.decorator.Bike; /** * @author KK JavaTutorails *Assemble Bike with Luxury features */ public class LuxuryBike extends DecoratorBike { public LuxuryBike(Bike bike) { super(bike); } @Override public void assembleBike() { super.assembleBike(); System.out.println("Adding featues of Luxury Bike.."); } } |
SportsBike.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials.decorator.impl; import com.kkjavatutorials.decorator.Bike; /** * @author KK JavaTutorails *Assemble Bike with Sports features */ public class SportsBike extends DecoratorBike { public SportsBike(Bike bike) { super(bike); } @Override public void assembleBike() { super.assembleBike(); System.out.println("Adding featues of Sports Bike.."); } } |
DecoratorBike.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kkjavatutorials.decorator.impl; import com.kkjavatutorials.decorator.Bike; /** * @author KK JavaTutorials * Decorator class helps to assemble bike with different features */ public class DecoratorBike implements Bike { private Bike bike; public DecoratorBike(Bike bike) { super(); this.bike = bike; } @Override public void assembleBike() { bike.assembleBike(); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kkjavatutorials.client; import com.kkjavatutorials.decorator.Bike; import com.kkjavatutorials.decorator.impl.BasicBike; import com.kkjavatutorials.decorator.impl.LuxuryBike; import com.kkjavatutorials.decorator.impl.SportsBike; /** * @author KK JavaTutorials *Client program which uses Decorator pattern */ public class ClientTest { public static void main(String[] args) { //Assemble Basic Bike System.out.println("-----------Assemble Basic Bike-------------"); Bike basicBike = new BasicBike(); basicBike.assembleBike(); //Decorate BasicBike with Luxury Feature System.out.println("-----------Decorate BasicBike with Luxury Feature------------"); Bike luxuryBike = new LuxuryBike(new BasicBike()); luxuryBike.assembleBike(); /*Decorate BasicBike with Luxury and Sports Features*/ System.out.println("------Decorate BasicBike with Luxury and Sports Features-----"); Bike sportsLuxuryBike = new SportsBike(new LuxuryBike(new BasicBike())); sportsLuxuryBike.assembleBike(); } } |
Output of this client Program:
———–Assemble Basic Bike————-
Basic Bike..
———–Decorate BasicBike with Luxury Feature————
Basic Bike..
Adding featues of Luxury Bike..
——Decorate BasicBike with Luxury and Sports Features—–
Basic Bike..
Adding featues of Luxury Bike..
Adding featues of Sports Bike..
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
That’s all about the Decorator Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.