-
Notifications
You must be signed in to change notification settings - Fork 2
/
Composite.ts
48 lines (39 loc) · 973 Bytes
/
Composite.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
abstract class DeliveryItem {
items: DeliveryItem[]
addItem(item: DeliveryItem) {
this.items.push(item)
}
getItemPrices(): number {
return this.items.reduce((acc: number, i: DeliveryItem) => acc += i.getItemPrices(), 0)
}
abstract getPrice(): number
}
class DeliveryShop extends DeliveryItem {
constructor(private delivaryFee: number) {
super()
}
getPrice(): number {
return this.items.reduce((acc: number, i: DeliveryItem) => acc += i.getItemPrices(), 0) + this.delivaryFee
}
}
class Package extends DeliveryItem {s
getPrice(): number {
return this.getItemPrices()
}
}
class Product extends DeliveryItem {
constructor(private price: number) {
super()
}
getPrice(): number {
return this.price
}
}
const shop = new DeliveryShop(100)
shop.addItem(new Product(1000))
const pack1 = new Package()
pack1.addItem(new Product(200))
pack1.addItem(new Product(300))
shop.addItem(pack1)
const pack2 = new Package()
pack2.addItem(new Product(30))