Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Decorator Design Pattern in Java

DZone's Guide to

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.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Let'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 a   Gadget  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.

Gadget.java
package com.genericclass.decorator.design.pattern;

public interface Gadget {
     Gadget addGadget();
    void showGadget();
}


GadgetDecorator.java
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(); 
   }
}


FlightGadget.java
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..."); 
  }
}


UnibeamChestProjectorGadget.java
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..."); 
  }
}


Suit.java
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...");
 } 
}


IronMan.java
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(); 
    }
 }


Main.java
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(); }
}


Output:
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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,design patterns ,tutorial ,decorator design pattern

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}