-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_pattern.js
35 lines (31 loc) · 892 Bytes
/
proxy_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
function CryptocurrencyAPI() {
this.getValue = function(coin) {
console.log("Calling External API...");
switch(coin) {
case "Bitcoin":
return "$8,500";
case "Litecoin":
return "$50";
case "Ethereum":
return "$175";
}
}
}
function CryptocurrencyProxy() {
this.api = new CryptocurrencyAPI();
this.cache = {};
this.getValue = function(coin) {
if(this.cache[coin] == null) {
this.cache[coin] = this.api.getValue(coin);
}
return this.cache[coin];
}
}
const proxy = new CryptocurrencyProxy();
console.log(proxy.getValue("Bitcoin"));
console.log(proxy.getValue("Litecoin"));
console.log(proxy.getValue("Bitcoin"));
console.log(proxy.getValue("Ethereum"));
console.log(proxy.getValue("Litecoin"));
console.log(proxy.getValue("Bitcoin"));
console.log(proxy.getValue("Ethereum"));