File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
patterns/memento/memento_editor Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments