Decorator Design Pattern in Java
Wanting to learn more about how to implement the decorator design pattern in Java? Check out this post to learn more about the decorator pattern in the Gadget interface.
Join the DZone community and get the full member experience.
Join For FreeLet's imagine that we want to create a game, and, for our superhero, we want to add a new gadget to every level of the game. Each gadget should be selected by the player. How can we do that?
One way to do that is by using the traditional inheritance. We will create a base abstract gadget class and multiple subclasses for different gadget combination.
But, wait! I have n numbers of gadgets, so, in my case, I have to create n subclasses.
If n = 5, then, for 5 powers, the number of subclasses will be 5! = 120. For 5 gadgets, we have to create 120 subclasses. Is there any other simple way to do that?
Yes, we can use the decorator design pattern to solve this problem.
The decorator design pattern allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
The decorator design pattern comes under the structural pattern. This pattern creates a decorator class that wraps around the original class and provides additional functionality, keeping class methods' signature intact.
We will create aGadget
interface and concrete classes implementing the
Gadget
interface. We will, then, create an abstract decorator class Gadgate
Decorator
implementing the
Gadget
interface and having
Gadget
object as its instance variable.

FlightGadget
and UnibeamChestProjectorGadget
are concrete classes implementing the GadgateDecorator
.
The suit class will implement the Gadget
interface, and it will be our base gadget or first gadget.
package com.genericclass.decorator.design.pattern;
public interface Gadget {
Gadget addGadget();
void showGadget();
}
package com.genericclass.decorator.design.pattern;
public abstract class GadgetDecorator implements Gadget{
Gadget nextGadget;
public GadgetDecorator(Gadget nextGadget) {
this.nextGadget = nextGadget;
}
@Override public
Gadget addGadget() {
return this;
}
@Override
public void showGadget() {
nextGadget.showGadget();
}
}
package com.genericclass.decorator.design.pattern;
public class FlightGadget extends GadgetDecorator {
public FlightGadget(Gadget nextGadget) {
super(nextGadget);
}
@Override
public void showGadget() {
super.showGadget();
System.out.println(" Flying Gadget...");
}
}
package com.genericclass.decorator.design.pattern;
public class UnibeamChestProjectorGadget extends GadgetDecorator {
public UnibeamChestProjectorGadget(Gadget nextGadget) {
super(nextGadget);
}
@Override
public void showGadget() {
super.showGadget();
System.out.println(" Unibeam Chest Projector...");
}
}
package com.genericclass.decorator.design.pattern;
public class Suit implements Gadget{
@Override
public Gadget addGadget() {
return this; }
@Override
public void showGadget() {
System.out.println(" Iron Man Suit...");
}
}
package com.genericclass.decorator.design.pattern;
public class IronMan {
Gadget gadget = new Suit();
public void addNewGadget(String gadgetName) {
if(gadgetName.equals("Flight"))
this.gadget = new FlightGadget(gadget);
else
this.gadget = new UnibeamChestProjectorGadget(gadget);
}
public void showGadget() {
System.out.println("My Gadgets:");
gadget.showGadget();
}
}
package com.genericclass.decorator.design.pattern;
public class Main {
public static void main(String[] args) {
IronMan ironMan = new IronMan();
ironMan.addNewGadget("Unibeam Chest Projector");
ironMan.addNewGadget("Flight");
ironMan.showGadget(); }
}
My Gadgets:
Iron Man Suit...
Unibeam Chest Projector...
Flying Gadget...
In conclusion, the decorator pattern provides greater flexibility than static inheritance. It enhances the extensibility of the object because changes are made by coding new classes.
The decorator pattern can be used when we want to add responsibilities dynamically at runtime.
Published at DZone with permission of Nitin Ranjan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments