Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. /**
  2. * Class presenting a box
  3. * @class
  4. */
  5. class Box {
  6. /**
  7. * Create a box
  8. * @param {Object} - box size (width, height)
  9. */
  10. constructor({ width, height }) {
  11. this.width = width;
  12. this.height = height;
  13. this.isOpened = false;
  14. }
  15.  
  16. /**
  17. * Open box by setting isOpened to true
  18. */
  19. openBox = () => {
  20. this.isOpened = true;
  21. console.log(`Box is ${this.isOpened ? 'opened' : 'closed'}`);
  22. }
  23.  
  24. /**
  25. * Close box by setting isOpened to false
  26. */
  27. closeBox = () => {
  28. this.isOpened = false;
  29. console.log(`Box is ${this.isOpened ? 'opened' : 'closed'}`);
  30. }
  31. }
  32.  
  33. /**
  34. * Class presenting a tape
  35. * @class
  36. */
  37. class Tape {
  38. /**
  39. * Create a piece of tape
  40. * @param {Number} length - length of tape to get
  41. */
  42. constructor(length=10) {
  43. this.length = length;
  44. this.isUsed = false;
  45. }
  46.  
  47. /**
  48. * Glue a tape to the box
  49. */
  50. glue = () => {
  51. this.isUsed = true;
  52. }
  53. }
  54.  
  55. /**
  56. * Class presenting a simple factory
  57. * @class
  58. */
  59. class DoorFactory {
  60. /**
  61. * @param {Object} params - size of the box to return
  62. * @return {Box} box object
  63. */
  64. static makeBox(params={width:10, height:10}) {
  65. console.log(`Creating box with size: width=${params.width}, height=${params.height}`);
  66. return new Box(params);
  67. }
  68.  
  69. /**
  70. * @param {Number} length - length of tape to return
  71. * @return {Tape} tape object
  72. */
  73. static getTape(length) {
  74. console.log(`Preparing tape with length: ${length}`);
  75. return new Tape(10);
  76. }
  77. }
  78.  
  79. // ==========================================================================
  80. // USAGE
  81. // ==========================================================================
  82.  
  83. const sizes = [10, 20, 30, 40, 50];
  84. const { makeBox, getTape } = DoorFactory;
  85.  
  86. const giftBoxesBySize = new Map(
  87. sizes.map(size => ([size, makeBox({width:size, height:size})]))
  88. );
  89. const typesForBoxes = new Map(
  90. sizes.map(size => ([size, getTape(size)]))
  91. );
  92.  
  93. console.log(giftBoxesBySize.values());
  94. console.log(typesForBoxes.values());
  95.  
  96.  
  97. // Creating box with size: width=10, height=10
  98. // Creating box with size: width=20, height=20
  99. // Creating box with size: width=30, height=30
  100. // Creating box with size: width=40, height=40
  101. // Creating box with size: width=50, height=50
  102. // Preparing tape with length: 10
  103. // Preparing tape with length: 20
  104. // Preparing tape with length: 30
  105. // Preparing tape with length: 40
  106. // Preparing tape with length: 50
  107. // MapIterator {Box, Box, Box, Box, Box}
  108. // MapIterator {Tape, Tape, Tape, Tape, Tape}
Add Comment
Please, Sign In to add comment