-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy_pattern.js
46 lines (39 loc) · 920 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
39
40
41
42
43
44
45
46
function Fedex()
{
this.calculate = ({ weight }) => {
return weight * 2.45;
}
}
function UPS()
{
this.calculate = ({ weight }) => {
return weight * 1.56;
}
}
function USPS()
{
this.calculate = ({ weight }) => {
return weight * 4.5;
}
}
function Shipping()
{
this.company = "";
this.setStrategy = (company) => {
this.company = company;
}
this.calculate = package => {
return this.company.calculate(package);
}
}
const fedex = new Fedex();
const ups = new UPS();
const usps = new USPS();
const package = { from: "Alabama", to: "Georgia", weight: 2 };
const shipping = new Shipping();
shipping.setStrategy(fedex);
console.log("Fedex: " + shipping.calculate(package));
shipping.setStrategy(ups);
console.log("UPS: " + shipping.calculate(package));
shipping.setStrategy(usps);
console.log("USPS: " + shipping.calculate(package));