Design Pattern Examples
Overview of object-oriented design patterns
Memento.cs
Go to the documentation of this file.
1
5
7{
13 public interface IMemento
14 {
21 string Name { get; }
22 }
23
24
25 //########################################################################
26 //########################################################################
27
28
36 public class Memento_TextObject
37 {
38 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
39 // Inner class Memento
40
46 internal class Memento : IMemento
47 {
52 string _name;
53
58 string _text;
59
60
66 internal Memento(string name, string text)
67 {
68 _name = name;
69 _text = text;
70 }
71
72
78 internal string Text
79 {
80 get
81 {
82 return _text;
83 }
84 }
85
90 string IMemento.Name
91 {
92 get
93 {
94 return _name;
95 }
96 }
97 }
98
99 // End of Inner class Memento
100 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
101 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102
103
104 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105 // Private data of the Memento_TextObject class.
106
110 string _text;
111
112 // End private data of the Memento_TextObject class.
113 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115
116
121 public Memento_TextObject(string text)
122 {
123 _text = text;
124 }
125
129 public string Text
130 {
131 get
132 {
133 return _text;
134 }
135 set
136 {
137 _text = value;
138 }
139 }
140
145 public IMemento GetMemento(string operationName)
146 {
147 return new Memento(operationName, _text);
148 }
149
150
156 public void RestoreMemento(IMemento memento)
157 {
158 _text = ((Memento)memento).Text;
159 }
160
161
167 public override string ToString()
168 {
169 return _text;
170 }
171 }
172}
Represents a single memento (snapshot) of the text state before an operation is applied....
Definition: Memento.cs:47
string _text
The snapshot of the text data as stored in the Memento_TextObject class instance.
Definition: Memento.cs:58
string _name
The name of this memento (really just the name of the operation that triggered the need for this meme...
Definition: Memento.cs:52
string Text
The saved text in this memento. This is accessible only by the Memento_TextObject class since it is t...
Definition: Memento.cs:79
Memento(string name, string text)
Constructor.
Definition: Memento.cs:66
Container for a string. Need to use a class that allows the text to be changed while the container (t...
Definition: Memento.cs:37
string _text
The text that can change in this Memento_TextObject class.
Definition: Memento.cs:110
void RestoreMemento(IMemento memento)
Sets the text in this class instance to the snapshot stored in the given IMemento object (which is as...
Definition: Memento.cs:156
Memento_TextObject(string text)
Constructs a text object with an initial string.
Definition: Memento.cs:121
override string ToString()
Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).
Definition: Memento.cs:167
string Text
Gets or sets the text in this TextObject.
Definition: Memento.cs:130
IMemento GetMemento(string operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
Definition: Memento.cs:145
Represents a single memento, a single snapshot of the state of the Memento_TextObject class as repres...
Definition: Memento.cs:14
string Name
The name of the memento (snapshot). Useful for displaying a list of mementos in an undo list....
Definition: Memento.cs:21
The namespace containing all Design Pattern Examples implemented in C#.