Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frontdevops/8e16625a26350d40d0d1 to your computer and use it in GitHub Desktop.
Save frontdevops/8e16625a26350d40d0d1 to your computer and use it in GitHub Desktop.
/**
* 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