Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Client-specified embellishment of a core object by recursively wrapping it.
Wrapping a gift, putting it in a box, and wrapping the box.
Example - Cars
We create few classes and then we can dynamically build cars, without changing the code.
Here is how to build Mustang and LaFerrari using the classes wrapping.
The code prints out the following and we can observe how car was assembled.
Last updated
Was this helpful?