Combination mode: Combine a group of objects into a tree structure, treat the combined object and the leaf object in a unified manner, and ignore the differences between them (because the leaf object can also contain leaf objects and become a combined object). The objects in the combination mode can only be a one-to-many relationship, and no many-to-one can occur.
Basic Unit: A combined object contains multiple leaf objects. Each basic unit can be a leaf object of another combination object. A folder or file can also be the content of other folders, but a folder or file cannot belong to multiple superior folders at the same time.
When implementing the combination mode in JavaScript, to ensure that the combined object and the leaf object have the same interface method, the operations of the same group of leaf objects must be consistent.
example:
//Define the combined object var Folder = function (name) {this.name = name;this.parent = null;this.files = [];};Folder.prototype.add = function (file) {file.parent = this;if(this.files.indexOf(file) === -1){this.files.push(file);}else{console.log('/'+file.name+'/' already exists, add failed');}};Folder.prototype.scan = function () {if(this.parent){console.log('Start scan/''+this.parent.name+'/': '+this.name);}else{console.log('Start scanning the root directory: '+this.name);}//The key is here, call the interface method of all its leaf objects scan()for(var i = 0, file; file = this.files[i++];){file.scan();}};Folder.prototype.remove = function (file) {var n = this.files.indexOf(file);if(n === -1){console.log('Cannot delete: /''+file.name+'/' does not exist:');}if(n >= 0){this.files.splice(n,1);console.log('Successfully deleted:'+file.name);}};//Define leaf object var File = function (name) {this.name = name;this.parent = null;};File.prototype.add = function () {console.log('can't be added below the file');};File.prototype.scan = function () {console.log(this.parent.name+': '+this.name);};File.prototype.remove = function (file) {console.log('can't be deleted: /''+file.name+'/'Do not exist:');};//Test var folder = new Folder('Directory');var folder1 = new Folder('Learn Material');var folder2 = new Folder('javascript');var file1 = new File('Node.js');var file2 = new File('qq.jpg');folder.add(folder1);folder.add(folder1);folder.add(folder2);folder1.add(file1);folder2.add(file2);//'Learning material' added successfully//'Learning material' already exists, but failed to add //'javascript' added successfully//'Node.js' added successfully//'qq.jpg' added successfully fold.remove(folder1);folder.remove(folder1);file1.remove(file1);//Successfully deleted: Learning material//Unable to delete: 'Learning material' does not exist://Unable to delete: 'Node.js' does not exist:folder.scan(); //This is equivalent to executing a macro command//Start scanning the root directory: Directory//Start scanning the 'directory': javascript//javascript: qq.jpgThe above is the JavaScript combination mode introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!