Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CryptocurrencyAPI {
- getValue(coin) {
- console.log("Calling external API...")
- switch(coin) {
- case "bitcoin": return "$2";
- case "littlecoin": return "$6";
- case "ethernum": return "$9";
- }
- }
- }
- class cryptocurrencyProxy {
- constructor() {
- this.api = new CryptocurrencyAPI()
- this.cache = {}
- }
- getValue(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("littlecoin"))
- console.log(proxy.getValue("ethernum"))
- console.log(proxy.getValue("bitcoin"))
- console.log(proxy.getValue("bitcoin"))
- console.log(proxy.getValue("ethernum"))
Add Comment
Please, Sign In to add comment