command pattern invoker invoker(invoker add_command(command invoker add_command(command invoker run(now you would be able to queue up commands to be executed even if the receiver were busy executing another command this is very useful concept in distributed systems where incoming commands need to be captured and handledbut the system may be busy with another action having all the information in an object in the queue allows the system to deal with all incoming commands without losing important commands while executing some other command you essentially get to dynamically create new behavior at runtime this process is especially useful when you want to set up the execution of method at one time and then sometime later execute it think about the implication for robot like the mars rover if operatives were only able to send single command to be executed and then had to wait for the command to finish execution before they could transmit the next command it would make doing anything on mars extremely time-consuming to the point of being impossible what can be done instead is to send sequence of actions to the rover the rover can then invoke each command in turnallowing it to get more work done per unit of timeand potentially negating much of the effect of the time lag between transmission and receipt of commands between the rover and earth this string of commands is called macro macros are also useful when running automated tests where you want to mimic user interaction with the systemor when you want to automate certain tasks that you do repeatedly you could create command for each step in the process and then string them up before letting some invoker handle the execution the decoupling allows you to swap out direct human intervention via the keyboard for generated computer input once we begin stringing commands togetherwe can also look at undoing the commands that were executed this is another area where the command pattern is particularly useful by pushing an undo command onto stack for every command that gets invokedwe get multi-level undo for freemuch like you see in everything from modern word processors to video-editing software to illustrate how you could go about using the command pattern to build multilevel undo stackwe are going to build very simple calculator that can do additionsubtractionmultiplicationand division for the sake of keeping the example simplewe will not concern ourselves with the case where we have multiplication by zero