pub fn memento_exercise() -> Result<(), String>
Expand description

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.