Lalit m Upadheyay

+ Follow
since Oct 09, 2014
Lalit m likes ...
Java Spring Ubuntu
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lalit m Upadheyay

Use JSON.parse

4 years ago

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 ?
After reading Head Fisrt Design Pattern, I am able to summarize following analogy :
A Button in a remote control => a ConcreteCommand
An Electrical Appliance => Receiver
RemoteAControl => Client (my undersatnding)
RemoteAControl => Invoker (as per HFDP)
A Slot in RemoteControl(corresponding to a set of buttons specific to an appliance) => Invoker , can be parameterized with different requests

Please comment if there is a mismatch in the analogy. Also I have few questions regarding Comamnd pattern :
Is invoker a logical entity ?
What is the role played by a remote control in Command pattern – Invoker, Client ? The book mentions it as Invoker. Then who is playing role of the client other than a class having public static void main ?
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.

My two options ::

version 1:

version 2 using mix of unicode and plain text in sourcecode for obfuscation