Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Last active January 19, 2016 20:20
Show Gist options
  • Save frontdevops/de279fb93a0e8a624bd5 to your computer and use it in GitHub Desktop.
Save frontdevops/de279fb93a0e8a624bd5 to your computer and use it in GitHub Desktop.
/**
* ConcreteComponent
* (класс для последующего декорирования)
*/
class Coffee {
getCost(defCost:number = 1):number {
return defCost;
}
}
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;
}
var coffee =
new Milk(
new Whip(
new Sprinkles(
new Coffee()
)
)
);
console.log(coffee.getCost());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment