DECORATOR DESIGN PATTERN

Let’s imagine we want to create a game, and for our superhero, we want to add a new gadget to every level of the game and gadget should be selected by the player.

How we can do¬†that?ūü§Ē

One way to do that use 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 gadget we have to create 120 subclasses.ūüėĘ

Is there any other simple way to do that?

Yes, We can use Decorator Design Pattern to solve this problem.

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

Decorator Design Pattern comes under structural pattern. This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

ironman.png

We will create a Gadget interface and concrete classes implementing the Gadget interface. We will then create an abstract decorator class GadgateDecorator implementing the Gadget interface and having Gadget object as its instance variable.

FlightGadget and UnibeamChestProjectorGadget are concrete classes implementing GadgateDecorator.

Suit class will implement Gadget interface and it will be our base gadget or first gadget.

Gadget.java

GadgetDecorator.java

FlightGadget.java

UnibeamChestProjectorGadget.java

Suit.java

IronMan.java

Main.java

Output:

Decorator Pattern provides greater flexibility than static inheritance. It enhances the extensibility of the object because changes are made by coding new classes.

Decorator Pattern can be used when we want to add responsibilities dynamically at runtime.

17 thoughts on “DECORATOR DESIGN PATTERN”

  1. You are a really persuasive writer. I can see this in your writeup. You’ve a way of writing compelling info that sparks significantly interest. adeefdeefdkg

  2. I and my pals were reading through the best guides on your web site and then instantly I had a terrible suspicion I had not expressed respect to the web blog owner for those techniques. Most of the men became for this reason stimulated to read all of them and have in effect honestly been taking pleasure in them. Thank you for actually being really accommodating and for making a decision on this sort of awesome information most people are really desirous to discover. My very own honest apologies for not expressing appreciation to you sooner.

  3. Thanks a lot for giving everyone an extraordinarily superb possiblity to check tips from this website. It’s always so awesome and also jam-packed with amusement for me and my office peers to visit your site at the least thrice in one week to read through the newest items you have got. And indeed, we are usually fulfilled with your exceptional things served by you. Selected two facts on this page are really the most effective we have ever had.

Leave a Reply

Your email address will not be published. Required fields are marked *