Undo/Redo mechanism

G

Guest

Hi,

I have a small graphics app and am trying to improve on the undo/redo
mecahism for the users. Currently jusy kee a track of the last 5 items added
or deleted and allow thos to be removed/re-added.

Any ideas on an undo/redo mechism or part of a class-design that will help
this process and make it more funcitional against all user actions?

Thanks,

Lini
 
S

Stefan Lieser

Hi Lini,

I solved Undo/Redo with the Command pattern. Every user operation results in
the creation of a command object. Each command is implemented by its own
class and inherits a base class called Command (or implements an interface
ICommand). On command creation the necessary information for Do and Undo are
saved in the newly created command object.

After creation the command object is added to a command stack where it is
execute. Execution is done by calling the parameterless Do method. Undo is
done by going down the stack and calling Undo. Redo is done by going up the
stack and calling Do again.


Sincerely,
Stefan Lieser
 
G

Guest

Stefan, GREAT idea!!

I will look into that right now!!

To the rest, any other great ideas for undo/redo?


Thanks!
 
G

Guest

Stefan,

I use vb.Net

What do I import/reference or declare to ge tto the Command pattern or
Command object?
Sorry, I'm not sure I can get to this in VB.

Any more advice?

Thanks
 
S

Stefan Lieser

Hi Lini,
What do I import/reference or declare to ge tto the Command pattern or
Command object?

well.... you should google for Command pattern. It's not as easy as
importing some assemblies, you have to program the necessary classes. There
are lots of examples on the web, so you have not to reinvent the wheel of
course ;-)


Sincerely,
Stefan Lieser
 
G

Guest

Hi Stefan,

Thanks again for pointing me in the right direction!
I use a derivative of the Factory pattern but didn't look any further...

Time to learn a WHOLE LOT more!

If you know of any VB samples, that'd be great.

Thanks again,

Lini
 
G

Guest

Heh Tim,

Great link and this has opened my eyes a lot!
Have used a derivative of the Factory Pattern but that as far as I went...

Any samples in VB you know of?

Thanks again,

Lini
 
T

Tim Wilson

No, sorry. The only samples that I'm currently aware of are in C#. However,
there are C# to VB.Net converters out on the Internet. Here is one
(http://csharpconverter.claritycon.com/Default.aspx). I don't know of any
converters that are perfect so I would advise you to pass a block of C# code
in at a time and then look it over to see the conversion that was made so
that maybe in the future you could translate without the help of a
converter. I find it particularly useful to have a good working knowledge of
other languages, even if I don't code in them, just so that I could
translate a sample if I needed to.
 
G

Guest

Hi Tim,

You're a big help! I can read the C# code in that link you sent...Not sure
if it is deep enough but that's not my technical critique showing, just
expected implementations of interfaces like ICommand etc which I can't seem
to get to from VB. Not sure what imports to use other than System.

Also my drawing class has so many commands, I cannot see how to store these
operations for the inevitable undo actions by the user. Phew, where to start
skinning this cat??

Gonna buy the book on Design Patterns for VB.Net or something like that...
anything that may help a bit more!

Thanks again,

Lini
 
T

Tim Wilson

I've never actually gone through the process of creating an undo/redo action
command set in code. Sounds interesting. I just came across the Command
pattern in the past and noticed that the "real-world code demonstrates" of
the Command pattern, at the bottom of the page, has a basic undo/redo
strategy. Anything more complicated might be too complicated for the given
task. The undo/redo example is pretty much entirely written from the ground
up, meaning that there is not much framework "help" in it aside from some
stuff out of the Collections namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top