Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created December 9, 2020 00:57
Show Gist options
  • Select an option

  • Save misterpoloy/62d7a9783d98db27b217a686829a077c to your computer and use it in GitHub Desktop.

Select an option

Save misterpoloy/62d7a9783d98db27b217a686829a077c to your computer and use it in GitHub Desktop.
Decorator Pattern
class FrozenYoghurt {
constructor(flavor, price) {
this.flavor = flavor
this.price = price
}
orderPlaced() {
console.log(`The ${this.flavor} flavor will cost you ${this.price} dollars`);
}
}
// decorator 1
function addFlavors(froyo) {
froyo.addStrawberry = true;
froyo.addVanilla = true;
froyo.price += 20;
froyo.updatedInfo = function(){
console.log(`The updated price after adding flavors is ${froyo.price} dollars`)
}
return froyo;
}
// decorator 2
function addToppings(froyo) {
froyo.hasSprinkles = true;
froyo.hasBrownie = true;
froyo.hasWafers = true;
froyo.allToppings = function(){
console.log("Your froyo has sprinkles, brownie, and wafers")
}
return froyo;
}
//using decorators
//creating a froyo
const froyo = new FrozenYoghurt("chocolate",10)
froyo.orderPlaced()
//adding flavors
var froyowithFlavors = addFlavors(froyo)
froyowithFlavors.updatedInfo()
//adding toppings
var froyoWithToppings = addToppings(froyo)
froyoWithToppings.allToppings()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment