You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: memento/README.md
+35-54Lines changed: 35 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,24 +9,30 @@ tags:
9
9
---
10
10
11
11
## Also known as
12
+
12
13
Token
13
14
14
15
## Intent
15
-
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored
16
-
to this state later.
16
+
17
+
Without violating encapsulation, capture and externalize an object's internal state so that the
18
+
object can be restored to this state later.
17
19
18
20
## Explanation
21
+
19
22
Real world example
20
23
21
-
> We are working on astrology application where we need to analyze star properties over time. We are creating snapshots of star state using Memento pattern.
24
+
> We are working on astrology application where we need to analyze star properties over time. We are
25
+
> creating snapshots of star state using Memento pattern.
22
26
23
27
In plain words
24
28
25
-
> Memento pattern captures object internal state making it easy to store and restore objects in any point of time.
29
+
> Memento pattern captures object internal state making it easy to store and restore objects in any
30
+
> point of time.
26
31
27
32
Wikipedia says
28
33
29
-
> The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
34
+
> The memento pattern is a software design pattern that provides the ability to restore an object to
35
+
> its previous state (undo via rollback).
30
36
31
37
**Programmatic Example**
32
38
@@ -38,23 +44,13 @@ public enum StarType {
38
44
RED_GIANT("red giant"),
39
45
WHITE_DWARF("white dwarf"),
40
46
SUPERNOVA("supernova"),
41
-
DEAD("dead star"),
42
-
UNDEFINED("");
43
-
44
-
privatefinalString title;
45
-
46
-
StarType(Stringtitle) {
47
-
this.title = title;
48
-
}
49
-
50
-
@Override
51
-
publicStringtoString() {
52
-
return title;
53
-
}
47
+
DEAD("dead star");
48
+
...
54
49
}
55
50
```
56
51
57
-
Next let's jump straight to the essentials. Here's the star class along with the mementos that we need manipulate.
52
+
Next, let's jump straight to the essentials. Here's the `Star` class along with the mementos that we
53
+
need manipulate. Especially pay attention to `getMemento` and `setMemento` methods.
58
54
59
55
```java
60
56
publicinterfaceStarMemento {
@@ -123,29 +119,8 @@ public class Star {
123
119
privateint ageYears;
124
120
privateint massTons;
125
121
126
-
publicStarTypegetType() {
127
-
return type;
128
-
}
129
-
130
-
publicvoidsetType(StarTypetype) {
131
-
this.type = type;
132
-
}
133
-
134
-
publicintgetAgeYears() {
135
-
return ageYears;
136
-
}
137
-
138
-
publicvoidsetAgeYears(intageYears) {
139
-
this.ageYears = ageYears;
140
-
}
141
-
142
-
publicintgetMassTons() {
143
-
return massTons;
144
-
}
145
-
146
-
publicvoidsetMassTons(intmassTons) {
147
-
this.massTons = massTons;
148
-
}
122
+
// setters and getters ->
123
+
...
149
124
}
150
125
}
151
126
```
@@ -172,27 +147,33 @@ And finally here's how we use the mementos to store and restore star states.
172
147
star.setMemento(states.pop());
173
148
LOGGER.info(star.toString());
174
149
}
175
-
176
-
// sun age: 10000000 years mass: 500000 tons
177
-
// red giant age: 20000000 years mass: 4000000 tons
178
-
// white dwarf age: 40000000 years mass: 32000000 tons
179
-
// supernova age: 80000000 years mass: 256000000 tons
180
-
// dead star age: 160000000 years mass: 2048000000 tons
181
-
// supernova age: 80000000 years mass: 256000000 tons
182
-
// white dwarf age: 40000000 years mass: 32000000 tons
183
-
// red giant age: 20000000 years mass: 4000000 tons
184
-
// sun age: 10000000 years mass: 500000 tons
185
150
```
186
151
152
+
Program output:
153
+
154
+
```
155
+
sun age: 10000000 years mass: 500000 tons
156
+
red giant age: 20000000 years mass: 4000000 tons
157
+
white dwarf age: 40000000 years mass: 32000000 tons
158
+
supernova age: 80000000 years mass: 256000000 tons
159
+
dead star age: 160000000 years mass: 2048000000 tons
160
+
supernova age: 80000000 years mass: 256000000 tons
161
+
white dwarf age: 40000000 years mass: 32000000 tons
162
+
red giant age: 20000000 years mass: 4000000 tons
163
+
sun age: 10000000 years mass: 500000 tons
164
+
```
187
165
188
166
## Class diagram
167
+
189
168

190
169
191
170
## Applicability
171
+
192
172
Use the Memento pattern when
193
173
194
-
* a snapshot of an object's state must be saved so that it can be restored to that state later, and
195
-
* a direct interface to obtaining the state would expose implementation details and break the object's encapsulation
174
+
* A snapshot of an object's state must be saved so that it can be restored to that state later, and
175
+
* A direct interface to obtaining the state would expose implementation details and break the
0 commit comments