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

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

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "helpers/formatstring.h"
#include "helpers/stack.h"
#include "helpers/replace.h"
#include "Memento_Memento.h"
#include "Memento_TextObject.h"
#include "Memento_Exercise.h"
Include dependency graph for Memento_Exercise.c:

Go to the source code of this file.

Functions

static void Memento_SaveForUndo (Memento_TextObject *text, const char *operation)
 Take a snapshot of the given text object associated with the name of given operation.
 
static void Memento_Operation_Replace (Memento_TextObject *source, const char *searchPattern, const char *replaceText)
 An operation to search and replace text in a Memento_TextObject.
 
static void Memento_Operation_Reverse (Memento_TextObject *source)
 An operation to reverse the characters in the given Memento_TextObject.
 
static void Memento_Undo (Memento_TextObject *text)
 Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list. If the undo list is empty, nothing happens.
 
static void Memento_ApplyReplaceOperation (Memento_TextObject *text, const char *searchPattern, const char *replaceText)
 Helper function to replace a pattern with another string in the given Memento_TextObject after adding a snapshot of the text object to the undo list. Finally, it shows off what was done.
 
static void Memento_ApplyReverseOperation (Memento_TextObject *text)
 Helper function to reverse the order of the characters in the given Memento_TextObject after adding a snapshot of the text object to an undo list. Finally, it shows what was done.
 
void Memento_Exercise (void)
 Example of using the Memento design pattern.
 

Variables

static StackEntry_mementoUndoList = NULL
 The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
 

Detailed Description

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

Definition in file Memento_Exercise.c.

Function Documentation

◆ Memento_ApplyReplaceOperation()

static void Memento_ApplyReplaceOperation ( Memento_TextObject text,
const char *  searchPattern,
const char *  replaceText 
)
static

Helper function to replace a pattern with another string in the given Memento_TextObject after adding a snapshot of the text object to the undo list. Finally, it shows off what was done.

Parameters
textThe Memento_TextObject to affect.
searchPatternWhat to look for in the Memento_TextObject.
replaceTextWhat to replace the searchPattern with.

Definition at line 118 of file Memento_Exercise.c.

References formatstring(), Memento_Operation_Replace(), Memento_SaveForUndo(), and Memento_TextObject_ToString().

Referenced by DesignPatternExamples_cpp::Memento_Exercise(), and Memento_Exercise().

◆ Memento_ApplyReverseOperation()

static void Memento_ApplyReverseOperation ( Memento_TextObject text)
static

Helper function to reverse the order of the characters in the given Memento_TextObject after adding a snapshot of the text object to an undo list. Finally, it shows what was done.

Parameters
textThe Memento_TextObject to affect.

Definition at line 144 of file Memento_Exercise.c.

References Memento_Operation_Reverse(), Memento_SaveForUndo(), and Memento_TextObject_ToString().

Referenced by DesignPatternExamples_cpp::Memento_Exercise(), and Memento_Exercise().

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

◆ Memento_Operation_Replace()

static void Memento_Operation_Replace ( Memento_TextObject source,
const char *  searchPattern,
const char *  replaceText 
)
static

An operation to search and replace text in a Memento_TextObject.

Parameters
sourceThe Memento_TextObject to affect.
searchPatternWhat to look for in the Memento_TextObject.
replaceTextWhat to replace the searchPattern with.

Definition at line 54 of file Memento_Exercise.c.

References Memento_TextObject_GetText(), Memento_TextObject_SetText(), and replace_str().

Referenced by Memento_ApplyReplaceOperation().

◆ Memento_Operation_Reverse()

static void Memento_Operation_Reverse ( Memento_TextObject source)
static

An operation to reverse the characters in the given Memento_TextObject.

Parameters
sourceThe Memento_TextObject to affect.

Definition at line 71 of file Memento_Exercise.c.

References Memento_TextObject_GetText().

Referenced by Memento_ApplyReverseOperation().

◆ Memento_SaveForUndo()

static void Memento_SaveForUndo ( Memento_TextObject text,
const char *  operation 
)
static

Take a snapshot of the given text object associated with the name of given operation.

Parameters
textThe Memento_TextObject to take a snapshot of.
operationA string describing the operation that will be applied after the snapshot is taken.

Definition at line 35 of file Memento_Exercise.c.

References _mementoUndoList, Memento_Create(), Memento_TextObject_GetText(), and Stack_Push().

Referenced by Memento_ApplyReplaceOperation(), and Memento_ApplyReverseOperation().

◆ Memento_Undo()

static void Memento_Undo ( Memento_TextObject text)
static

Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list. If the undo list is empty, nothing happens.

Parameters
textThe Command_TextObject to affect.

Definition at line 94 of file Memento_Exercise.c.

References _mementoUndoList, Memento_Destroy(), Memento_TextObject_SetText(), Memento_TextObject_ToString(), Memento::name, Stack_Pop(), and Memento::text.

Referenced by DesignPatternExamples_cpp::Memento_Exercise(), and Memento_Exercise().

Variable Documentation

◆ _mementoUndoList

StackEntry* _mementoUndoList = NULL
static

The list of memento objects that form a series of snapshots in time of a Memento_TextObject.

Definition at line 26 of file Memento_Exercise.c.

Referenced by DesignPatternExamples_cpp::Memento_Exercise(), Memento_Exercise(), Memento_SaveForUndo(), and Memento_Undo().