Last active
January 19, 2016 20:20
-
-
Save frontdevops/de279fb93a0e8a624bd5 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 { | |
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