Created
January 19, 2016 19:57
-
-
Save frontdevops/8e16625a26350d40d0d1 to your computer and use it in GitHub Desktop.
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
/** | |
* ConcreteComponent | |
* (класс для последующего декорирования) | |
*/ | |
class Coffee { | |
private cost:number = 1; | |
getCost():number { return this.cost; } | |
} | |
abstract class CoffeeItem { | |
protected cost:number; | |
constructor(protected coffee:Coffee) { | |
} | |
public getCost():number { | |
return this.coffee.getCost() + this.cost; | |
} | |
} | |
/** | |
* Decorator A | |
*/ | |
class Milk extends CoffeeItem { | |
protected cost = 0.5; | |
} | |
/** | |
* Decorator B | |
*/ | |
class Whip extends CoffeeItem { | |
protected cost = 0.7; | |
} | |
/** | |
* Decorator C | |
*/ | |
class Sprinkles extends CoffeeItem { | |
protected cost = 0.2; | |
} | |
function decorate(...args) { | |
let newObj = args.shift(); | |
for (let obj of args) { | |
if (typeof obj != 'function') continue; | |
newObj = new obj(newObj); | |
} | |
return newObj; | |
} | |
var coffee = new Coffee(); | |
var myCoffee = decorate(coffee, Milk, Whip, Sprinkles); | |
console.log(myCoffee.getCost()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment