Question

I have a project for my university where I'm using PowerDesigner to model an application that should be an abstract MS Visio, basically a generic graph editor.

I'm supposed to implement the command pattern, but focusing only on the actual work space (and not, say, the hierarchy tree). Now, I have an abstract class that has an abstract function called execute() and an empty (but not abstract) function named undo(). The idea is to have the concrete commands override the first method, but only have the undoable commands override the second method.

I'm having some trouble in understanding what qualifies as a command. The undoable methods are somewhat easy to name, as I have Move, Resize, Rotate, AlterProperty, Delete and Add. The commands that aren't undoable are the problem.

Is every action on the toolbar a command? I was thinking of putting Zoom and Scroll as a command, but I'm not sure if that even makes sesne. As for Cut, Copy and Paste, the first is pretty much a filling of the Clipboard element list, after which the Delete command is called, while the latter two are pretty much adding and removing from the Clipboard list, so I don't know if I should classify that as a command.

Basically, the question is - what do I do with commands that aren't undoable? Should every action create a command object?

Was it helpful?

Solution

Zoom/Scroll - why do you consider them not undoable? Undo-Zoom -> set the zoom level before the change. Undo-Scroll -> move the position back. In each case, they'd store the current state when created. Whether you'd want them in the undo-queue is another story.

Cut / Copy / Paste - in general, in terms of undo I don't expect the clipboard state to change. But you absolutely do expect that the Delete from the Cut and the Adds from the Paste are undoable. If you copy N items and then paste, is that one Add command, or multiple? If it's multiple, you might want Cut / Paste to be meta commands that apply a list of Add subcommands - otherwise, if you paste N items, you'd have to undo N times instead of once. If it's a single Add, then it's somewhat redundant, though it may be nice for display (e.g. "Undo paste" vs. "Undo add...")

Same logic goes for Cut.

The short answer is: you could have every action be a command, if it makes sense in your code. But it may be overkill - you could also separate between the model and the view, and only have things that actually alter the model be a command / undoable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top