Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undo/redo with cascading deletions

I'm trying to implement an undo/redo feature into my application, using the Command Pattern. I'm facing a problem.

To illustrate it, let's imagine you can create with my application 2D profiles (as many as you want).

From these 2D profiles, you can then create a 3D part with different attributes (name, colour, scale, etc.).

+--------------+              +--------------+      +--------------+
| 2D profile A |              | 2D profile B |      | 2D profile C |
+--------------+              +--------------+      +--------------+
   |    |                            |
   |  +---------------+      +---------------+
   |  | 3D Part B     |      | 3D Part C     |
   |  | Colour : blue |      | Colour : grey |
   |  | Name : bibi   |      | Name : foo    |
   |  | Scale : 33%   |      | Scale : 100%  |
   |  +---------------+      +---------------+
+--------------+
| 3D Part A    |
| Colour : red |
| Name : aaa   |
| Scale : 50%  |
+--------------*

When a profile is deleted, all 3D parts which a built on this profile are automaticaly deleted too (when a profile is about to be deleted, a 3D part manager is notified and will delete the obsolete 3D parts. Views are also notified to update the GUI).

This is where I'm facing a problem : I'm writing the undo/redo command for deleting a 2D profile, which looks something like this (pseudo-code) :

virtual void redo()
{
    m_pProfileList.remove(m_pProfile); // This will automatically delete all 3D parts relying on the deleted 2D profile
}

virtual void undo()
{
   m_pProfileList.add(m_pProfile); // This will add the 2D profile, but the 3D Parts are lost
}

As you can see in the code above, removing the 2D profile will automatically delete all 3D parts relying on the removed profile.

But when doing undo, re-adding the 2D profile to the list is not enough : the 3D parts are lost.

What should I do ? Should the undo/redo command be responsible of taking care of the deletion of the 3D parts (which is something actually done by the 3d part manager) ? This would mean the undo/redo command would also be responsible to notify the views to update the GUI.

Or should the undo/redo command create an internal copy of all 3d parts which will be deleted and let the 3d part manager delete the 3D parts ?

Or is there another better solution ?

Thanks for your help !

like image 683
Jérôme Avatar asked Jan 12 '09 15:01

Jérôme


1 Answers

You want a slight variation on this: the Memento pattern. You store snapshots of either your complete object tree or just all the differences at each change. Armed with this successive history of changes, you can then go backwards and forward in through commands to your hearts content, without losing dependant objects.

like image 99
GaryF Avatar answered Jan 05 '23 02:01

GaryF