I develop a graphical editor in C++/Qt, that uses a tree of objects as its data model. The object’s classes also form a class hierarchy. You can think of a tree of widgets in some kind of GUI framework, like Java Swing or Qt. Now I want to implement an undo/redo-functionality. Therefore I need to track every modification to both the tree nodes (e.g. changing object properties) and also the tree itself (e.g. adding, deleting nodes). Unfortunately the tree objects will also be modified by third party code and I have to ensure, that this code does not break my application.
So, considering that the tree will be HUGE (several 100,000 nodes) how can I ensure, that I can see every modification to the tree? It should also be noted, that the tree nodes contain a lot of modifiable properties.
The Command Pattern is one way to do it. However every modification to the data needs to be done via a Command object and logged by the undo system. It’s the sort of thing that is best included from the beginning. It could be quite a pain to retrofit.
If you can’t intercept the modifications by the third-party code and turn them into Commands, you’ll probably just have to take a snapshot of everything before each change. There are obvious problems with this if your data structure is large. You could have a special Command on your undo stack for these that only stores such snapshots for third-party modifications.