Design Pattern Examples
Overview of object-oriented design patterns
Memento_TextObject Class Reference

Container for a string. Need to use a class that allows the text to be changed while the container (this class) remains constant. This way, operations can be applied to the text and the container's contents change but not the container (strings are immutable in C#; this dances around that problem). More...

Collaboration diagram for Memento_TextObject:
Collaboration graph

Classes

class  Memento
 Represents a single memento (snapshot) of the text state before an operation is applied. The operation becomes the name of the memento for display purposes. More...
 

Public Member Functions

 Memento_TextObject (string text)
 Constructs a text object with an initial string.
 
IMemento GetMemento (string operationName)
 Returns an IMemento object containing a snapshot of the text stored in this class instance.
 
void RestoreMemento (IMemento memento)
 Sets the text in this class instance to the snapshot stored in the given IMemento object (which is assumed to be from this class).
 
override string ToString ()
 Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).
 

Properties

string Text [get, set]
 Gets or sets the text in this TextObject.
 

Private Attributes

string _text
 The text that can change in this Memento_TextObject class.
 

Detailed Description

Container for a string. Need to use a class that allows the text to be changed while the container (this class) remains constant. This way, operations can be applied to the text and the container's contents change but not the container (strings are immutable in C#; this dances around that problem).

Definition at line 36 of file Memento.cs.

Constructor & Destructor Documentation

◆ Memento_TextObject()

Memento_TextObject ( string  text)
inline

Constructs a text object with an initial string.

Parameters
textThe initial string for the text object.

Definition at line 121 of file Memento.cs.

References Memento_TextObject._text.

Member Function Documentation

◆ GetMemento()

IMemento GetMemento ( string  operationName)
inline

Returns an IMemento object containing a snapshot of the text stored in this class instance.

Definition at line 145 of file Memento.cs.

References Memento_TextObject._text.

Referenced by Memento_Exercise.Memento_SaveForUndo().

◆ RestoreMemento()

void RestoreMemento ( IMemento  memento)
inline

Sets the text in this class instance to the snapshot stored in the given IMemento object (which is assumed to be from this class).

Parameters
mementoThe IMemento object to restore to.

Definition at line 156 of file Memento.cs.

References Memento_TextObject._text.

Referenced by Memento_Exercise.Memento_Undo().

◆ ToString()

override string ToString ( )
inline

Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).

Returns
Returns the text stored in the text object.

Definition at line 167 of file Memento.cs.

References Memento_TextObject._text.

Member Data Documentation

◆ _text

Property Documentation

◆ Text

string Text
getset

Gets or sets the text in this TextObject.

Definition at line 129 of file Memento.cs.

Referenced by Memento_Exercise.Memento_Operation_Replace(), and Memento_Exercise.Memento_Operation_Reverse().


The documentation for this class was generated from the following file: