There are cases where we might want to add some extra functionality to a class in an application. This could be done via inheritance; however, we might not want to do this or it may affect all the other classes in our application. This is where the decorator design pattern is useful.
The purpose of the decorator design pattern is to add functionality to objects without extending them and without affecting the behavior of other objects from the same class.
The decorator design pattern works by wrapping the decorated object, and it can be applied during runtime. Decorators are extremely useful in the cases where there could be multiple extensions of a class and they could be combined in various ways. Instead of writing all the possible combinations, decorators can be created and they can stack the modifications on top of each other. The next few subsections...