js 命令模式 组合模式
* 基本宏命令
var closeDoorCommand = { execute: function() { console.log("Closing the door..."); } }; var openPcCommand = { execute: function() { console.log("Opening the PC..."); } }; var launchQQCommand = { execute: function() { console.log("launching QQ..."); } }; var MacroCommand = function() { return { commandsList: [], add: function(command) { this.commandsList.push(command); }, execute: function() { for (var i = 0, command; command = this.commandsList[i]; i++) { command.execute(); } } } }; var macroCommand = MacroCommand(); macroCommand.add(closeDoorCommand); macroCommand.add(openPcCommand); macroCommand.add(launchQQCommand); macroCommand.execute(); // Closing the door... // Opening the PC... // launching QQ...
* 树形宏命令
<html> <head> <meta charset="UTF-8"> <title>macro command</title> </head> <body> <button id="button">Press me</button> <script> var MacroCommand = function() { return { commandsList: [], add: function(command) { this.commandsList.push(command); }, execute: function() { for (var i = 0, command; command = this.commandsList[i]; i++) { command.execute(); } } } }; // 打开空调 var openAcCommand = { execute: function() { console.log("Opening the Air conditioner..."); } }; // 打开电视和音响 var openTvCommand = { execute: function() { console.log("Opening the TV..."); }, add: function() { throw new Error('Cannot add child node to leaf object'); } }; var openStereoCommand = { execute: function() { console.log("Opening the stereo..."); } }; var macroCommand1 = MacroCommand(); macroCommand1.add(openTvCommand); macroCommand1.add(openStereoCommand); // 关门、开电脑、登陆qq var closeDoorCommand = { execute: function() { console.log("Closing the door..."); } }; var openPcCommand = { execute: function() { console.log("Opening the PC..."); } }; var launchQQCommand = { execute: function() { console.log("launching QQ..."); } }; var macroCommand2 = MacroCommand(); macroCommand2.add(closeDoorCommand); macroCommand2.add(openPcCommand); macroCommand2.add(launchQQCommand); // 现在把所有的命令组合成一个超级命令 var macroCommand = MacroCommand(); macroCommand.add(openAcCommand); macroCommand.add(macroCommand1); macroCommand.add(macroCommand2); // 绑定到遥控器 var setCommand = (function(command) { document.getElementById("button").onclick = function() { command.execute(); } })(macroCommand); // openTvCommand.add(macroCommand); // Uncaught Error: </script> </body> </html>
运行结果:
* 扫描文件夹
/********** Folder **************/ function Folder(name) { this.name = name; this.files = []; } Folder.prototype.add = function(file) { this.files.push(file); } Folder.prototype.scan = function() { console.log("开始扫描文件夹: " +this.name); for (var i = 0, file, files = this.files; file = files[i]; i++) { file.scan(); } } /********** File **************/ function File(name) { this.name = name; } File.prototype.add = function() { throw new Error("文件下面不能再添加文件"); } File.prototype.scan = function() { console.log("开始扫描文件: " +this.name); } var folder = new Folder('学习资料'); var folder1 = new Folder('javascript'); var folder2 = new Folder('jQuery'); var file1 = new File('javascript设计模式与开发实践'); var file2 = new File('精通jQuery'); var file3 = new File('重构与模式'); folder1.add(file1); folder2.add(file2); folder.add(folder1); folder.add(folder2); folder.add(file3); var folder3 = new Folder('Nodejs'); var file4 = new File('深入浅出Node.js'); folder3.add(file4); var file5 = new File('javascript语言精髓与编程实践'); folder.add(folder3); folder.add(file5); folder.scan(); /* 开始扫描文件夹: 学习资料 开始扫描文件夹: javascript 开始扫描文件: javascript设计模式与开发实践 开始扫描文件夹: jQuery 开始扫描文件: 精通jQuery 开始扫描文件: 重构与模式 开始扫描文件夹: Nodejs 开始扫描文件: 深入浅出Node.js 开始扫描文件: javascript语言精髓与编程实践 */
* 引用父对象
/********** Folder **************/ function Folder(name) { this.name = name; this.parent = null; // add attribute this.parent this.files = []; } Folder.prototype.add = function(file) { file.parent = this; // set parent object this.files.push(file); } Folder.prototype.scan = function() { console.log("开始扫描文件夹: " +this.name); for (var i = 0, file, files = this.files; file = files[i]; i++) { file.scan(); } } Folder.prototype.remove = function() { if (!this.parent) { // root node or free node return; } for (var files = this.parent.files, i = files.length -1; 0 <= i; i--) { var file = files[i]; if (file === this) { files.splice(i, 1); } } } /********** File **************/ function File(name) { this.name = name; } File.prototype.add = function() { throw new Error("文件下面不能再添加文件"); } File.prototype.scan = function() { console.log("开始扫描文件: " +this.name); } File.prototype.remove = function() { if (!this.parent) { // root node or free node return; } for (var files = this.parent.files, i = files.length -1; 0 <= i; i--) { var file = files[i]; if (file === this) { files.splice(i, 1); } } } var folder = new Folder('学习资料'); var folder1 = new Folder('javascript'); var file1 = new File('深入浅出Node.js'); folder1.add(new File('javascript语言精髓与编程实践')); folder.add(folder1); folder.add(file1); folder1.remove(); folder.scan();