Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Last active January 19, 2016 20:17
Show Gist options
  • Save frontdevops/16acdc144007eeb1d10e to your computer and use it in GitHub Desktop.
Save frontdevops/16acdc144007eeb1d10e to your computer and use it in GitHub Desktop.
/**
* ConcreteComponent
* (класс для последующего декорирования)
*/
class Coffee {
private cost :number = 1;
getCost() :number {
return this.cost;
}
}
/**
* Decorator A
*/
function milkDecorator() :void {
this.cost += 0.5;
}
/**
* Decorator B
*/
function whipDecorator() :void {
this.cost += 0.7;
}
/**
* Decorator C
*/
function sprinklesDecorator() :void {
this.cost += 0.2;
}
function decorate<T>(...args) :T {
let target = args.shift();
args.map((decor) => decor.apply(target));
return target;
}
var coffee = new Coffee();
var myCoffee = decorate<Coffee>(
coffee,
milkDecorator,
whipDecorator,
sprinklesDecorator
);
console.log(myCoffee.getCost());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment