-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.ts
More file actions
58 lines (48 loc) · 1.35 KB
/
Polymorphism.ts
File metadata and controls
58 lines (48 loc) · 1.35 KB
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
type Purchase = any;
let Logistics: any;
interface Delivery {
deliverProduct();
trackProduct();
}
class DeliveryImplementation {
protected purchase: Purchase;
constructor(purchase: Purchase) {
this.purchase = purchase;
}
}
class ExpressDelivery extends DeliveryImplementation implements Delivery {
deliverProduct() {
Logistics.issueExpressDelivery(this.purchase.product);
}
trackProduct() {
Logistics.trackExpressDelivery(this.purchase.product);
}
}
class InsuredDelivery extends DeliveryImplementation implements Delivery {
deliverProduct() {
Logistics.issueInsuredDelivery(this.purchase.product);
}
trackProduct() {
Logistics.trackInsuredDelivery(this.purchase.product);
}
}
class StandardDelivery extends DeliveryImplementation implements Delivery {
deliverProduct() {
Logistics.issueStandardDelivery(this.purchase.product);
}
trackProduct() {
Logistics.trackStandardDelivery(this.purchase.product);
}
}
function createDelivery(purchase) {
if (this.purchase.deliveryType === 'express') {
delivery = new ExpressDelivery(purchase);
} else if (this.purchase.deliveryType === 'insured') {
delivery = new InsuredDelivery(purchase);
} else {
delivery = new StandardDelivery(purchase);
}
return delivery
}
let delivery: Delivery = createDelivery({});
delivery.deliverProduct();