Guest User

Untitled

a guest
Aug 20th, 2019
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Example 1
  2. function add(x, y) { return x + y; }
  3. function sub(x, y) { return x - y; }
  4. function mul(x, y) { return x * y; }
  5. function div(x, y) { return x / y; }
  6.  
  7. const Command = function (execute, undo, value) {
  8. this.execute = execute;
  9. this.undo = undo;
  10. this.value = value;
  11. }
  12.  
  13. const AddCommand = function (value) {
  14. return new Command(add, sub, value);
  15. };
  16.  
  17. const SubCommand = function (value) {
  18. return new Command(sub, add, value);
  19. };
  20.  
  21. const MulCommand = function (value) {
  22. return new Command(mul, div, value);
  23. };
  24.  
  25. const DivCommand = function (value) {
  26. return new Command(div, mul, value);
  27. };
  28.  
  29. const Calculator = function () {
  30. let current = 0;
  31. let commands = [];
  32. let index = 0;
  33.  
  34. function action(command) {
  35. let name = command.execute.toString().substr(9, 3);
  36. return name.charAt(0).toUpperCase() + name.slice(1);
  37. }
  38.  
  39. return {
  40. execute: function (command) {
  41. current = command.execute(current, command.value);
  42. commands.push(command);
  43. index++;
  44. console.log(action(command) + ": " + command.value);
  45. },
  46.  
  47. undo: function () {
  48. if (index > 0) {
  49. let command = commands[index - 1];
  50. current = command.undo(current, command.value);
  51. index--;
  52. console.log("Undo " + action(command) + ": " + command.value);
  53. }
  54. },
  55. redo: function () {
  56. if (index < commands.length) {
  57. let command = commands[index];
  58. current = command.execute(current, command.value);
  59. index++;
  60. console.log("Redo " + action(command) + ": " + command.value);
  61. }
  62. },
  63.  
  64. getCurrentValue: function () {
  65. return current;
  66. }
  67. }
  68. }
  69.  
  70. const calculator = new Calculator();
  71.  
  72. // issue commands
  73.  
  74. calculator.execute(new AddCommand(100));
  75. calculator.execute(new SubCommand(24));
  76. calculator.execute(new MulCommand(6));
  77. calculator.execute(new DivCommand(2));
  78. calculator.undo();
  79. calculator.undo();
  80. calculator.redo();
  81. calculator.redo();
  82. calculator.redo();
  83.  
  84. console.log(`Answer: ${calculator.getCurrentValue()}`);
  85.  
  86. //Example 2
  87. const carManager = {
  88. // request information
  89. requestInfo: function (model, id) {
  90. return "The information for " + model + " with ID " + id + " is foobar";
  91. },
  92. // purchase the car
  93. buyVehicle: function (model, id) {
  94. return "You have successfully purchased Item " + id + ", a " + model;
  95. },
  96. // arrange a viewing
  97. arrangeViewing: function (model, id) {
  98. return "You have successfully booked a viewing of " + model + " ( " + id + " ) ";
  99. }
  100. };
  101.  
  102. carManager.execute = function (name) {
  103. return carManager[name] && carManager[name].apply(carManager, [].slice.call(arguments, 1));
  104. };
  105. console.log(carManager.execute("buyVehicle", "Ford Escort", "453543"));
RAW Paste Data