1 /** 2 * PriceCache 3 */ 4 function PriceCache() { 5 } 6 7 PriceCache.prototype = { 8 getCachedPrice: function(instrumentId) { 9 }, 10 setCachedPrice: function(instrumentId, price) { 11 } 12 } 13 14 /** 15 * PriceFetcher 16 */ 17 function PriceFetcher() { 18 } 19 20 PriceFetcher.prototype = { 21 getPriceFromServer: function(instrumentId) { 22 } 23 } 24 25 26 /** 27 * PriceService 28 */ 29 function PriceService(priceFetcher, priceCache) { 30 this._priceFetcher = priceFetcher; 31 this._priceCache = priceCache; 32 } 33 34 PriceService.prototype = { 35 getPrice: function(instrumentId) { 36 var price = this._priceCache.getCachedPrice(instrumentId); 37 if(price==null) { 38 price = this._priceFetcher.getPriceFromServer(instrumentId); 39 this._priceCache.setCachedPrice(instrumentId, price); 40 } 41 return price; 42 } 43 } 44