mituri

[JS] Wzorzec Posrednik

Apr 19th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1.  
  2. function AutoCoder(){
  3. this.getMaxSpeed = function(carName) {
  4. switch(carName) {
  5. case "bmw": return 320; break;
  6. case "mercedes": return 340; break;
  7. default : 0; break;
  8. }
  9. }
  10. }
  11.  
  12. var diary = (function(){
  13. var diary = '';
  14. return{
  15. add: function(msg){ diary += msg + "\n"; },
  16. show: function(){
  17. cl(diary);
  18. diary = "";
  19. }
  20. }
  21. })();
  22.  
  23. function AutoProxy(){
  24. var autoCode = new AutoCoder();
  25. var autoCache = {};
  26.  
  27. return {
  28. getmaxSpeed: function(carName) {
  29. if(!autoCache[carName]) {
  30. autoCache[carName] = autoCoder.getMaxSpeed(carName);
  31. }
  32.  
  33. diary.add(carName +": " + autoCache[carName]);
  34. return autoCache[carName];
  35. },
  36.  
  37. getCount: function(){
  38. var count = 0,
  39. code;
  40. for (code in autoCache) {
  41. ++count;
  42. }
  43. return count;
  44. }
  45. }
  46.  
  47. }
  48.  
  49. //var autoCoder = new AutoCoder();
  50. //autoCoder.getMaxSpeed("bmw");
  51.  
  52. //cl(carCalc.getMaxSpeed("mercedes"));
  53.  
  54. /*
  55. var autoProxy = new AutoProxy();
  56. autoProxy.getmaxSpeed('bmw');
  57. autoProxy.getmaxSpeed('mercedes');
  58.  
  59. diary.add("cache size: " + autoProxy.getCount());
  60. diary.show();
  61. */
Add Comment
Please, Sign In to add comment