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...

  

* 树形宏命令




    
    macro command



    
    



  

 

  

运行结果:

 

* 扫描文件夹

 

/********** 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();
 

  

  

 

你可能感兴趣的:(js 命令模式 组合模式)