-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-5-memento.vala
50 lines (35 loc) · 956 Bytes
/
3-5-memento.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class EditorMemento {
protected string content;
public EditorMemento (string content) {
this.content = content;
}
public string get_content () {
return content;
}
}
class Editor {
protected string content = "";
public void type (string words) {
content = content + " " + words;
}
public string get_content () {
return content;
}
public EditorMemento save () {
return new EditorMemento (content);
}
public void restore (EditorMemento memento) {
content = memento.get_content ();
}
}
public int main (string[] args) {
var editor = new Editor ();
editor.type ("This is the first sentence.");
editor.type ("This is second.");
var saved = editor.save ();
editor.type ("And this is third.");
print ("%s\n", editor.get_content ());
editor.restore (saved);
print ("%s\n", editor.get_content ());
return 0;
}