Search code examples
c#undo-redocommand-pattern

c# command pattern design issue


Dears,

I have object model whose members' (objects') properties can be changed by PropertyGrid and UI (PopupMenu etc). I am looking for support UndoRedo in this case and interested design of Command Pattern. AS I understand, changing any property via PropertyGrid should be registered in UndoRedo stack, however, Undoing this operation is equals to changing that property to old value - and Infinite Loop? Do you know any best practice in this case?

Thanks.


Solution

  • I suppose the Undo action doesn't have to be count as a change.

    You have a collection of your changes. When you make a change you add an item in this collection. If you make an undo you just swap the current state with [i-1] state. If you make a redo you swap the current state with [i+1] state. If you have made some Undo's and then make a change you start to write from there and rewrite all possible Redo's.