javascriptコマンドモード


javascriptコマンドモード
従来のオブジェクト言語に向けたコマンドモード

    
    
    


    var button1 = document.getElementById('button1');
    var button2 = document.getElementById('button2');
    var button3 = document.getElementById('button3');

    var setCommand = function (button, command) {
        button.onclick = function () {
            command.execute();
        }
    };

    //     
    var MenuBar = {
        refresh: function () {
            console.log('      ')
        }
    };

    var SubMenu = {
        add: function () {
            console.log('     ');
        },
        del: function () {
            console.log('     ');
        }
    };

    //     
    var RefreshMenuBarcOMMAND = function (receiver) {
        this.receiver = receiver;
    };

    RefreshMenuBarcOMMAND.prototype.execute = function () {
        this.receiver.refresh();
    };



    var AddSubMenuCommand = function (receiver) {
        this.receiver = receiver;
    };

    AddSubMenuCommand.prototype.execute = function () {
        this.receiver.add();
    };


    var DelSubMenuCommand = function (receiver) {
        this.receiver = receiver;
    };

    DelSubMenuCommand.prototype.execute = function () {
        this.receiver.del();
    }

    var refreshMenuBarCommand = new RefreshMenuBarcOMMAND(MenuBar);
    var addSubMenuCommand = new AddSubMenuCommand(SubMenu);
    var delSubMenuCommand = new DelSubMenuCommand(SubMenu);

    setCommand(button1, refreshMenuBarCommand);
    setCommand(button2, addSubMenuCommand);
    setCommand(button3, delSubMenuCommand);

javascriptのコマンドモード

    
    
    


    var button1 = document.getElementById('button1');
    var button2 = document.getElementById('button2');
    var button3 = document.getElementById('button3');


    var setCommand = function (button, command) {
        button.onclick = function () {
            command.execute();
        }
    };

    var MenBar = {
        refresh: function () {
            console.log("    ");
        }
    };

    //    
    var RefreshMenuBarCommand = function (receiver) {
        return {
            execute:  function () {
                receiver.refresh();
            }
        }
    };

    var refresBarCommand = RefreshMenuBarCommand(MenBar);

    setCommand(button1, refresBarCommand);

コマンドの取消しまたは再実行
var Ryu = {
    attack: function () {
        console.log('  ');
    },
    defense: function () {
        console.log('  ');
    },
    jump: function () {
        console.log('  ');
    },
    crouch: function () {
        console.log('  ');
    }
};

var makeCommand = function (receiver, state) {      //    
    return function () {
        var fun = receiver[ state ];
        fun();
    }
};

var commands = {
    '119': 'jump',          //W
    '115': 'crouch',        //S
    '97': 'defense',        //A
    '100': 'attack'         //D
};

var commandStack = [];              //       

document.onkeypress  = function (event) {
    var keyCode = event.which || event.keyCode,
            command = makeCommand(Ryu, commands[keyCode]);

    if(command){
        command();                  //    
        commandStack.push(command);        //              
    }
};

document.getElementById('replay').onclick = function () {       //      
    var command;
    while (command = commandStack.shift()){                     //           
        command();
    }
};
マクロコマンド
ユーザは、一連のコマンドを実行するように命令します.これがマクロコマンドです.
execute: function () {
        console.log('  ');
    }
};

var openPcCommand = {
    execute: function () {
        console.log('    ');
    }
};

var openQQCommand = {
    execute: function () {
        console.log('  QQ');
    }
};

//    ,            
var MacroCommand = function () {
    return {
        commandList: [],
        add: function (command) {
            this.commandList.push(command);
        },
        execute: function () {
            for(var i = 0, command; command = this.commandList[i++];){
                command.execute();
            }
        }
    }
};

var macrocommand = MacroCommand();
macrocommand.add(closeDoorCommand);
macrocommand.add(openPcCommand);
macrocommand.add(openQQCommand);

macrocommand.execute();