-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.ts
65 lines (51 loc) · 1.37 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
interface CoffeeComponent {
getPrice(): number;
}
class DefaultCoffeeImpl implements CoffeeComponent {
getPrice(): number {
return 5000;
}
}
class CoffeeDecorator implements CoffeeComponent {
protected coffee: CoffeeComponent;
public CoffeeDecorator(coffee: CoffeeComponent) {
this.coffee = coffee;
}
getPrice(): number {
return this.coffee.getPrice();
}
}
class MilkCoffee extends CoffeeDecorator {
constructor(coffee: CoffeeComponent) {
super();
this.coffee = coffee;
}
MilkCoffee(coffee: CoffeeComponent) {
this.coffee = coffee;
}
getPrice(): number {
return super.getPrice() + this.decorateWithMilk();
}
decorateWithMilk(): number {
return 3000;
}
}
class CheeseCoffee extends CoffeeDecorator {
constructor(coffee: CoffeeComponent) {
super();
this.coffee = coffee;
}
public CheeseCoffee(coffee: CoffeeComponent) {
this.coffee = coffee;
}
getPrice(): number {
return super.getPrice() + this.decorateWithCheese();
}
decorateWithCheese(): number {
return 7000;
}
}
const concreteComponent = new DefaultCoffeeImpl();
const milkCoffee = new MilkCoffee(concreteComponent);
const cheeseCoffee = new CheeseCoffee(concreteComponent);
const milkcheeseCoffee = new CheeseCoffee(milkCoffee);