Skip to content

Commit 3ae4283

Browse files
committed
Add README.md.
1 parent 2b74748 commit 3ae4283

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Memento pattern
2+
Memento is a behavioral design pattern that lets you save and restore the previous state of an
3+
object without revealing the details of its implementation.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/memento).
6+
7+
### Online demo:
8+
Click on the picture to see a [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/memento/flutter_memento_editor).
9+
10+
[![image](https://user-images.githubusercontent.com/8049534/165401175-88bc4593-4624-45b4-8c03-6f1390ed771a.png)](https://refactoringguru.github.io/design-patterns-dart/#/memento/flutter_memento_editor)
11+
12+
13+
### Dependency Patterns
14+
This complex example includes these implementations:
15+
- [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/app_observer)]
16+
- [[SubscriberWidget](https://github.com/RefactoringGuru/design-patterns-dart/tree/master/patterns/observer/subscriber_flutter_widget)]
17+
18+
### Diagram:
19+
![image](https://user-images.githubusercontent.com/8049534/165399085-06835617-8ef1-4e2f-930f-03d730433afb.png)
20+
21+
### Client code:
22+
```dart
23+
class MementoEditorApplication {
24+
final editor = Editor();
25+
final caretaker = Caretaker();
26+
27+
void createDefaultShapes() {/*...*/}
28+
29+
void saveState() {
30+
final snapshot = editor.backup();
31+
32+
if (caretaker.isSnapshotExists(snapshot)) {
33+
return;
34+
}
35+
36+
final memento = Memento(DateTime.now(), snapshot);
37+
caretaker.addMemento(memento);
38+
editor.events.notify(MementoCreateEvent());
39+
}
40+
41+
void restoreState(Memento memento) {
42+
editor
43+
..unSelect()
44+
..restore(memento.snapshot)
45+
..repaint();
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)