Memento Pattern

Posted by admin on April 11th, 2009

Yesterday I tried to implement Undo functionality with Command Patterns. The Memento pattern can also be used to do this. So what is the difference between the two?

Well, with Memento pattern the state of an object (the originator) is being stored by a Caretaker class. So if it’s possible to Undo with returning back to a previous state, use Memento pattern. Sometimes however, the originator is too complex to save each time its state (it could be a composite for instance). In that case the undo facility can use commands. Whenever an action is received a compensating action is set up and stored in the undo facility.

Example in Flex of Memento Pattern. I haven’t added too much comments because the code and example should speak for itself.

thx, Lieven Cardoen

Related: Command Pattern Undo Functionality

Pattern for Undo functionality

Posted by admin on April 10th, 2009

Yesterday someone asked me how to implement Undo functionality.  I knew there was a pattern commonly used for this, but couldn’t remember it. In my common sense I would keep track of previous states of properties (or actions). After some search I found that Command pattern is a good choice to implement the Undo functionality.

With some online examples I implemented an undo functionality pattern in Flex. Doing such an exercise makes me understand the theory behind it better.

Here’s the first example. Right click on the swf to view the source.

Next is implementing redo functionality in it. In a third step I’ll try to make some things more generic so I can use it easily in our product.

What would also be interesting is to make the example work in a way that only whole words are undone instead of single characters. I’ll also look into that later on.

thx, Lieven Cardoen

Related: Memento Pattern

pixel Pattern for Undo functionality

Copyright © 2007 Lieven Cardoen. All rights reserved.