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