码迷,mamicode.com
首页 > 其他好文 > 详细

组合模式

时间:2019-03-10 20:33:45      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:class   图片   one   slist   ++   comm   dad   组合模式   push   

技术图片
// 组合模式

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(‘打开空调‘);
    }
};

var openTvCommand = {
    execute : function () {
        console.log(‘打开电视‘);
    }
};

var openSoundCommand = {
    execute : function () {
        console.log(‘打开音响‘);
    }
};

var macorCommand = MacroCommand();
macorCommand.add(openTvCommand);
macorCommand.add(openSoundCommand);

var mainCommand = MacroCommand();

mainCommand.add(macorCommand);
mainCommand.add(openAcCommand);

mainCommand.execute();
View Code

 

组合模式

标签:class   图片   one   slist   ++   comm   dad   组合模式   push   

原文地址:https://www.cnblogs.com/tujw/p/10506811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!