Design Pattern Examples
Overview of object-oriented design patterns
c/Memento_Exercise.h File Reference

Declaration of the Memento_Exercise() function as used in the Memento Pattern. More...

This graph shows which files directly or indirectly include this file:

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.
 

Detailed Description

Declaration of the Memento_Exercise() function as used in the Memento Pattern.

Definition in file c/Memento_Exercise.h.

Macro Definition Documentation

◆ __MEMENTO_EXERCISE_H__

#define __MEMENTO_EXERCISE_H__

Definition at line 9 of file c/Memento_Exercise.h.

Function Documentation

◆ Memento_Exercise()

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().