Declaration of the Memento_Exercise() function as used in the Memento Pattern. More...
Go to the source code of this file.
Macros | |
#define | __MEMENTO_EXERCISE_H__ |
Functions | |
void | Memento_Exercise (void) |
Example of using the Memento design pattern. | |
Declaration of the Memento_Exercise() function as used in the Memento Pattern.
Definition in file c/Memento_Exercise.h.
#define __MEMENTO_EXERCISE_H__ |
Definition at line 9 of file c/Memento_Exercise.h.
void Memento_Exercise | ( | void | ) |
Example of using the Memento design pattern.
In this exercise, the Memento pattern is used to take snapshots of a text object so as to form an undo list of changes to the text object. Undoing an operation means restoring a snapshot of the text object.
The undo list is implemented as a stack of memento objects that each represent a snapshot of the text object taken before each operation is applied. After all operations are applied, the mementos are used to restore the text object in reverse order, effectively undoing each operation in turn.
Compare this to the Command_Exercise() and note that the steps taken there are identical to here (except for method names, of course). The difference lies in how operations are executed and undone. Mementos make the undo process much cleaner and faster since operations do not need to be applied repeatedly to get the text object into a specific state. Specifically, compare Command_Undo() with Memento_Undo(). Also note the differences in the "Memento_ApplyXXOperation()" methods, which more cleanly separate the save from the operation.
Definition at line 180 of file Memento_Exercise.c.
References _mementoUndoList, Memento_ApplyReplaceOperation(), Memento_ApplyReverseOperation(), Memento_TextObject_Create(), Memento_TextObject_Destroy(), Memento_TextObject_ToString(), and Memento_Undo().