Is anyone confused about Mosh’s implementation of undo using the command pattern? His explanation was that this is better because it’s less expensive but the way he’s implemented the command pattern for undo feature is actually doing the same thing? If you call boldCommand.execute() multiple times, you are storing the state unnecessarily. I guess it works if users are prevented to call execute more than once? is anyone else confuse like me too?
Hi!
First of all, I am no expert, but I believe Mosh actually wanted to use a stack (first in, last out like a stack of plates) instead of a deque, The pop method on the deque class removes from the front (head), not the back (tail). Alternatively he could probably use the removeLast method from the deque class which would make deque behave like a stack and not a queue.
I think it actually depends on how the commands are implemented. For example, if every time the command is called, the current content is wrapped in a bold tag, then it is not redundant. Stepping through that for a few calls, suppose we start with the string “content”:
content
<b>content</b>
<b><b>content</b></b>
<b><b><b>content</b></b></b>
You see how there is no redundancy on multiple calls?
And what about a command like “ToggleBoldCommand” which wraps or unwraps a bolded content based on the outermost tag? That would certainly not have redundant state if called multiple times.