Page 145 - JavaScript
P. 145

};
         };

         // log helper

         var log = (function() {
             var log = "";

             return {
                 add: function(msg) { log += msg + "\n"; },
                 show: function() { alert(log); log = ""; }
             }
         })();

         function run() {
             var yoko = new Participant("Yoko");
             var john = new Participant("John");
             var paul = new Participant("Paul");
             var ringo = new Participant("Ringo");

             var chatroom = new Chatroom();
             chatroom.register(yoko);
             chatroom.register(john);
             chatroom.register(paul);
             chatroom.register(ringo);

             yoko.send("All you need is love.");
             yoko.send("I love you John.");
             paul.send("Ha, I heard that!");

             log.show();
         }


        Command


        The command pattern encapsulates parameters to a method, current object state, and which
        method to call. It is useful to compartmentalize everything needed to call a method at a later time.
        It can be used to issue a "command" and decide later which piece of code to use to execute the
        command.


        There are three components in this pattern:

            1.  Command Message - the command itself, including the method name, parameters, and state
            2.  Invoker - the part which instructs the command to execute its instructions. It can be a timed
              event, user interaction, a step in a process, callback, or any way needed to execute the
              command.
            3.  Reciever - the target of the command execution.




        Command Message as an Array


         var aCommand = new Array();
         aCommand.push(new Instructions().DoThis);  //Method to execute
         aCommand.push("String Argument");  //string argument
         aCommand.push(777);                //integer argument
         aCommand.push(new Object {} );     //object argument


        https://riptutorial.com/                                                                             102
   140   141   142   143   144   145   146   147   148   149   150