-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.js
More file actions
59 lines (44 loc) · 1.3 KB
/
Copy pathExample2.js
File metadata and controls
59 lines (44 loc) · 1.3 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
59
// old interface
function Shipping() {
this.request = function(zipStart, zipEnd, weight) {
/**********/
return '$49.75';
}
}
// new interface
function AdvencedShipping() {
this.login = function(credentials) {}
this.setStart = function(start) {}
this.setDestination = function(destination) {}
this.calculate = function(weight) { return '$39.50'; }
}
function ShippingAdapter(credentials) {
let shipping = new AdvencedShipping();
shipping.login(credentials);
return {
request: function(zipStart, zipEnd, weight) {
shipping.setStart(zipStart);
shipping.setDestination(zipEnd);
return shipping.calculate(weight);
}
}
}
let log = (function() {
let log = "";
return {
add: function(msg) { log += msg + "\n";},
show: function() { console.log(log); log = "";}
}
})();
function run() {
let shipping = new Shipping();
let credentials = {token: "30a8-6ee1"};
let adapter = new ShippingAdapter(credentials);
let cost = shipping.request("78701", "10010", "2 lbs");
log.add("Old cost: " + cost);
// new shipping object with adapted interface
cost = adapter.request("78701", "10010", "2 lbs");
log.add("New cost: " + cost);
log.show();
}
run();