-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemento.dart
39 lines (28 loc) · 932 Bytes
/
Memento.dart
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
import 'package:design_pattern_dart/Display/Example.dart';
class Memento extends Example {
Memento([String filePath = "lib/Behavioral/Memento.dart"]) : super(filePath);
@override
String testRun() {
Editor editor = Editor();
// 打兩行字然後存檔。
editor.type("First Line.");
editor.type("Second Line.");
var saved = editor.save();
// 現在不小心按到鍵盤了,趕快復原。
editor.type("AAAAAAAAAAAAAAAAAAAAAAAAA");
editor.restore(saved);
return editor._content;
}
}
// 建立一個 Memento 用來記錄
class EditorMemento {
String _content;
EditorMemento(this._content);
}
// editor 就可以使用 memento 來存檔或復原了 !
class Editor {
String _content = "";
void type(String content) => _content += '\n' + content;
EditorMemento save() => EditorMemento(_content);
String restore(EditorMemento memento) => _content = memento._content;
}