-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy-pattern.js
38 lines (31 loc) · 914 Bytes
/
strategy-pattern.js
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
/* The Strategy pattern is a behavioral design pattern that enables you to define a group (or family) of closely-related algorithms (known as strategies).
The strategy pattern allows you to swap strategies in and out for each other as needed at runtime. */
class Flipkart {
cost = 100;
shippingCost(amount) {
return amount * this.cost;
}
}
class Amazon {
cost = 250;
shippingCost(amount) {
return amount * this.cost;
}
}
class Meesho {
cost = 30;
shippingCost(amount) {
return amount * this.cost;
}
}
class Shipping {
shippingCompany = ''
constructor(company) {
this.shippingCompany = company ? company : null;
}
calculate(amount) {
return this.shippingCompany ? this.shippingCompany.shippingCost(amount) : 0;
}
}
const flipkartShipping = new Shipping(new Flipkart);
console.log(flipkartShipping.calculate(10))