DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Decorator Design Pattern in Java

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.

Nitin Ranjan user avatar by
Nitin Ranjan
·
Aug. 03, 18 · Tutorial
Like (12)
Save
Tweet
Share
18.64K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Design Java (programming language)

Published at DZone with permission of Nitin Ranjan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Getting a Private SSL Certificate Free of Cost
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Keep Your Application Secrets Secret
  • 5 Steps for Getting Started in Deep Learning

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: