mercredi 4 octobre 2017

Memento or Command Pattern?

I have a JavaScript web app (Backbone for my models, ReactJS for my views) which has two 'display modes', which the user can toggle between, using a button. These modes render data from my model - which is a tree structure, implementing the Composite Pattern - by rearranging leaf objects within the hierarchy, moving multiple objects between containers on the same 'level' in the tree (think of a drawer containing boxes, and moving objects around from one box to another in the same drawer). Often this entails a complete rearrangement of the data. Currently I am handling this with the Memento Pattern, serializing and saving the current model until the user toggles the display mode again, when it is restored.

At the same time I also want to allow the user to make sequences of what can be thought of 'edits' in the usual sense - a sequence of arbitrary, redoable changes to objects, altering their size, colour, shape, etc. So it's a standard undo-redo pattern. I haven't written any code for this yet, but I am wondering exactly how I should implement this with respect to the Memento approach I already have in place for toggling between display modes. Is this something entirely different, or can/should I integrate the two?

One situation that possibly suggests the latter is this: the user might make an edit, which they wish to save, whilst in a display mode which will be toggled back to the previously stored one, which will now contain stale data. This suggests managing display mode changes and edit sequences together, thinking of them in terms of user commands, rather than app states (presumably modelling this in terms of the Command Pattern). Presumably on this pattern I would have a set of 'paired' commands for the display modes, which, rather than using a serializing pattern like Memento, know how to structure my underlying data in the appropriate ways and switch back and forth between these representations. Is this the right approach? Am I reasoning about this correctly?

N.B: My models are quite small, usually no more than a few Kb, and even with user interaction wouldn't grow much beyond that. So the Memento Pattern doesn't cost me anything. It's just whether it's right to continue using it in the place where it's implemented.

Aucun commentaire:

Enregistrer un commentaire