-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decorator.ts
46 lines (37 loc) · 1016 Bytes
/
Decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
interface Coffee {
cost(): Number;
}
class GeneralCoffee implements Coffee {
cost(): Number {
return 10;
}
}
class CoffeeExtraDecorator implements Coffee {
private _coffee: Coffee;
constructor(coffee: GeneralCoffee) {
this._coffee = coffee;
}
cost(): Number {
return this._coffee.cost();
}
}
class BubbleDecorator extends CoffeeExtraDecorator {
private _price: Number = 3;
cost(): Number {
return super.cost().valueOf() + this._price.valueOf();
}
}
class MilkDecorator extends CoffeeExtraDecorator {
private _price: Number = 2.5;
private _freshExtra: Number = 1.5;
cost(): Number {
return super.cost().valueOf() + this._price.valueOf() + this._freshExtra.valueOf();
}
}
// USAGE:
const general = new GeneralCoffee();
const withBubble = new BubbleDecorator(general);
const withMilk = new MilkDecorator(withBubble);
console.log(`Total: ${withMilk.cost()}`);
// OUTPUT:
// "Total: 17"