Decorator
Last updated
Last updated
Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
In JavaScript, the Decorator design pattern allows you to add new functionality to an object dynamically without altering its structure.
This pattern is useful when you want to extend the behavior of individual objects without creating a new subclass for each new functionality.
In this example, Coffee
is the base component, and WithMilk
and WithSugar
are decorators that add functionality to the base component.
Each decorator wraps around the base component and modifies its behavior by adding its own cost.