javascript combination pattern

Combination mode

**// Composite Patterns Form Tree Structures Between Objects
// Basic objects and composite objects are treated consistently in combination patterns
// You don't need to care how many layers of calls an object has to make at the root **
It combines command mode and combination mode.

    const MacroCommand = function () {
      return {
        lists: [],
        add: function (task) {
          this.lists.push(task)
        },
        excute: function () { //Composite objects call excute here
          for (let i = 0; i < this.lists.length; i++) {
            this.lists[i].excute()
          }
        }
      }
    }
    const command1 = MacroCommand() //Basic object
    command1.add({
      excute: () => console.log('Cook Coffee') //Basic object calls excute here
    })
    const command2 = MacroCommand()
    command2.add({
      excute: () => console.log('Turn on the TV.')
    })
    command2.add({
      excute: () => console.log('Turn on the stereo.')
    })
    const command3 = MacroCommand()
    command3.add({
      excute: () => console.log('Turn on the air conditioner')
    })
    command3.add({
      excute: () => console.log('Turn on the computer')
    })
    console.log(command1.lists) //The corresponding memory address is different
    console.log(command2.lists) //So lists are saved differently
    console.log(command3.lists)

    const macroCommand = MacroCommand()
    macroCommand.add(command2) //Put different parameters in stages
    macroCommand.add(command3) // The reference of a function remains unchanged, corresponding to the same 
                                 //A pile
     macroCommand.add(command1)
     //Finally, we put command1, 2, 3 into the array and call excute to trigger each number. 
      //excute method in group
    macroCommand.excute()

demo2 - Scan folders
When scanning folders, the folders below can be another folder or a file. We want to treat these folders and files uniformly. This situation is suitable for using combination mode.

   const Folder = function(folder){
    this.folder = folder
    this.lists = []
   }
   Folder.prototype.add = function(resource){
     this.lists.push(resource)
   }
   Folder.prototype.scan = function(){
     console.log('Start scanning folders',this.folder)
     for(let i =0,folder; folder = this.lists[i++];){
       folder.scan()
     }
   }
   const File = function(file){
     this.file = file
   }
   File.prototype.add = function(){
     throw Error('No other folders or files can be added under the file')
   }
   File.prototype.scan = function(){
     console.log('Start scanning files',this.file)
   }
   const folder = new Folder('Root file plus')
   const folder1 = new Folder('JS')
   const folder2 = new Folder('life')
   const file1 = new File('js Technology stack')
   const file2 = new File('authority')
   const file3 = new File('Princeling')
   folder1.add(file1)
   folder1.add(file2)
   folder2.add(file3)
   folder.add(folder1)
   folder.add(folder2)
   folder.scan()

Tags: Javascript

Posted on Wed, 09 Oct 2019 03:22:32 -0700 by docpepper