Why do we have undo as a shared global command ? Each Command object has its own implementation of undo() method and actually knows how to undo last request.
It was a design decision. We can keep an array of dedicated undoCommand objects that would be occupying space in heap memory or we can keep a shared undo command object without much memory overhead. Keeping a dedicated array of undo command would serve only one purpose - ability to undo any sequence of execution of command in any order.
The shared undo command will be initialized to the latest Command object(just after its execute is invoked) .
The sole purpose of undo command is to undo execution effect of latest command object. and that would surely mean redo as well if last command was kind of delete, close, switchoff, stop requests.
If we just press undo without any command being in effect, nothing would happen as all command objects have been initialized with NoCommand( a Null Object pattern).
If we press undo after a Command execution is in effect, its execution will be reverted and last available state will be re-stored.
If we press undo consecutively second or third time after a Command execution was in effect, nothing would happen as the effect has already been reverted on the very first execution of undo command.
I am able to analyze some of above questions - RemoteControl is an Invoker. An Invoker holds reference to command and at some point asks the command to carry out a request by calling its execute() method.
The client creates Invoker Object, Receiver Objects, Command Objects, binds the Receiver to the Command and adds reference of Comamnd objects to the Invoker Object. Later it uses the Invoker object to carry out specific request. So a Client class having public void static main is fine unless we are really looking for a fancier client .
Still trying to resolve queries around slot on the remote. Can someone please try to analyze with me ?