Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class presenting a box
- * @class
- */
- class Box {
- /**
- * Create a box
- * @param {Object} - box size (width, height)
- */
- constructor({ width, height }) {
- this.width = width;
- this.height = height;
- this.isOpened = false;
- }
- /**
- * Open box by setting isOpened to true
- */
- openBox = () => {
- this.isOpened = true;
- console.log(`Box is ${this.isOpened ? 'opened' : 'closed'}`);
- }
- /**
- * Close box by setting isOpened to false
- */
- closeBox = () => {
- this.isOpened = false;
- console.log(`Box is ${this.isOpened ? 'opened' : 'closed'}`);
- }
- }
- /**
- * Class presenting a tape
- * @class
- */
- class Tape {
- /**
- * Create a piece of tape
- * @param {Number} length - length of tape to get
- */
- constructor(length=10) {
- this.length = length;
- this.isUsed = false;
- }
- /**
- * Glue a tape to the box
- */
- glue = () => {
- this.isUsed = true;
- }
- }
- /**
- * Class presenting a simple factory
- * @class
- */
- class DoorFactory {
- /**
- * @param {Object} params - size of the box to return
- * @return {Box} box object
- */
- static makeBox(params={width:10, height:10}) {
- console.log(`Creating box with size: width=${params.width}, height=${params.height}`);
- return new Box(params);
- }
- /**
- * @param {Number} length - length of tape to return
- * @return {Tape} tape object
- */
- static getTape(length) {
- console.log(`Preparing tape with length: ${length}`);
- return new Tape(10);
- }
- }
- // ==========================================================================
- // USAGE
- // ==========================================================================
- const sizes = [10, 20, 30, 40, 50];
- const { makeBox, getTape } = DoorFactory;
- const giftBoxesBySize = new Map(
- sizes.map(size => ([size, makeBox({width:size, height:size})]))
- );
- const typesForBoxes = new Map(
- sizes.map(size => ([size, getTape(size)]))
- );
- console.log(giftBoxesBySize.values());
- console.log(typesForBoxes.values());
- // Creating box with size: width=10, height=10
- // Creating box with size: width=20, height=20
- // Creating box with size: width=30, height=30
- // Creating box with size: width=40, height=40
- // Creating box with size: width=50, height=50
- // Preparing tape with length: 10
- // Preparing tape with length: 20
- // Preparing tape with length: 30
- // Preparing tape with length: 40
- // Preparing tape with length: 50
- // MapIterator {Box, Box, Box, Box, Box}
- // MapIterator {Tape, Tape, Tape, Tape, Tape}
Add Comment
Please, Sign In to add comment