combination mode: combine a group of objects into a tree structure, and treat grouped objects and leaf objects uniformly, ignoring the differences between them (because leaf objects can also contain leaf objects and become composite objects), objects in the combined mode can only be one-to-many relationships and cannot appear on multiple pairs.
basic unit: A Composite object contains multiple leaf objects. Each basic unit can be a leaf object of another composite object like a network of folders and its contents, a folder or file can be the content of other folders, but a folder or file cannot belong to multiple parent folders at the same time.
When you implement a combination pattern in JavaScript, make sure that the combined object and leaf object have the same interface method, and that the operations on the same set of leaf objects must be consistent.
Example:
Defines the composite 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+ ' \ ' Existing, add failed '); Folder.prototype.scan = function () {if (this.parent) {console.log (' Start scan \ ' +this.parent.name+ ' \ ': ' +this.name);} else{Console.log (' Start scanning root: ' +this.name);}//Key here, invoke all its leaf object's interface method scan () for (var i = 0, file; file = this.files[i++];) {fi
Le.scan ();
}
}; Folder.prototype.remove = function (file) {var n = this.files.indexOf (file); if (n = = 1) {Console.log (' cannot delete: \ ' +file.nam
E+ ' \ ' does not exist: ');}
if (n >= 0) {this.files.splice (n,1); Console.log (' Successful deletion: ' +file.name ';}};
Defines the leaf object var File = function (name) {this.name = name; this.parent = null;};
File.prototype.add = function () {Console.log (' cannot be added under file ');
File.prototype.scan = function () {Console.log (this.parent.name+ ': ' +this.name);}; File.prototype.remove = function (fiLe) {console.log (' cannot delete: \ ' +file.name+ ' \ ' does not exist: ');
Test var folder = new folder (' directory ');
var folder1 = new Folder (' learning materials ');
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 materials ' add success/' learning materials ' already exist, add failed//' JavaScript ' Add success/' Node.js ' Add success/' qq.jpg ' Add success folder.remove (Folder1);
Folder.remove (Folder1);
File1.remove (FILE1); Successful deletion: Learning materials//Can not be deleted: ' Learning materials ' does not exist://cannot be deleted: ' node.js ' does not exist: Folder.scan (); This is equivalent to executing a macro command//start scan root directory: Directory//Start scan ' directory ': JavaScript//javascript:qq.jpg
The above is a small set to introduce the JavaScript combination mode, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!