Created
May 25, 2016 13:55
-
-
Save raduGaspar/88aad81dc0c09de91d4259199d1f2ef4 to your computer and use it in GitHub Desktop.
Decorator design pattern example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Design Patterns: Decorator | |
class Drink { | |
constructor() { | |
this.total = function() { | |
return 0; | |
} | |
} | |
} | |
// decorator 1 | |
const Rum = (drink) => { | |
let t = drink.total(); | |
drink.total = () => { | |
return t + 4; | |
} | |
} | |
// decorator 2 | |
const Sugar = (drink) => { | |
let t = drink.total(); | |
drink.total = () => { | |
return t + .5; | |
} | |
} | |
// decorator 3 | |
const Lime = (drink) => { | |
let t = drink.total(); | |
drink.total = () => { | |
return t + 1.5; | |
} | |
} | |
// decorator 4 | |
const Soda = (drink) => { | |
let t = drink.total(); | |
drink.total = () => { | |
return t + 3; | |
} | |
} | |
let drink = new Drink(); | |
Rum(drink); | |
Sugar(drink); | |
Lime(drink); | |
Soda(drink); | |
console.log(`Mojito price: $${drink.total()}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment