Decorator Pattern
Decorator Pattern
Section titled “Decorator Pattern”Introduction
Section titled “Introduction”The Decorator pattern lets you add behavior to objects dynamically without affecting other objects of the same class.
Implementation
Section titled “Implementation”class Coffee { cost() { return 5; } description() { return 'Coffee'; }}
// Decoratorsfunction withMilk(coffee) { return { cost: () => coffee.cost() + 1.5, description: () => coffee.description() + ' + Milk' };}
function withSugar(coffee) { return { cost: () => coffee.cost() + 0.5, description: () => coffee.description() + ' + Sugar' };}
function withWhippedCream(coffee) { return { cost: () => coffee.cost() + 2, description: () => coffee.description() + ' + Whipped Cream' };}
let myCoffee = new Coffee();myCoffee = withMilk(myCoffee);myCoffee = withSugar(myCoffee);
console.log(myCoffee.description()); // 'Coffee + Milk + Sugar'console.log(myCoffee.cost()); // 7Summary
Section titled “Summary”- Adds behavior dynamically without subclassing
- Wraps objects with additional functionality
- More flexible than inheritance