System Design ( Day - 49 )

Decorator Design Pattern
Definition
Decorator Pattern attaches additional responsibilities to an object dynamically, Decorator provides flexible alternative to subclassing for extending functionality.
Enhancing the functalalIties of the Objects by wrapping up with the decorator classes like a method in a class is returning the text “I Did Something“ then we have to wrap it up with the decorator class and update that text like “I Did Something Amazing“ for this decorator also we can wrap up with another decorator class and update the text to “I Did Something Amazing Today“.
We’ll take an example of Mario Game.
one abstract class Character with a method getAbilities( ), the concrete class would be MarioCharacter and getAbilities( ) that is defined in the Mario class and also we’ll have Decorator class which is abstract class that decorator class also has the concrete classes which are HighUpDec, GunPowerDec,StarPowDec these are the abilities that can be enhanced, this chain can be long chain of combinations. this is nothing but a recursive relationship.
Standard UML for Decorator Pattern.
Real-World Example
In the Text editor we have to decorate the text right there we’ll use this to bold the text or to change the style to italic, the text is already there we only need to update with the decorator.
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
