Skip to content

Commit 74360a7

Browse files
committed
Update README.md
1 parent 3544a83 commit 74360a7

File tree

2 files changed

+36
-56
lines changed

2 files changed

+36
-56
lines changed

memento/README.md

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ tags:
99
---
1010

1111
## Also known as
12+
1213
Token
1314

1415
## 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.
1719

1820
## Explanation
21+
1922
Real world example
2023

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.
2226
2327
In plain words
2428

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.
2631
2732
Wikipedia says
2833

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).
3036
3137
**Programmatic Example**
3238

@@ -38,23 +44,13 @@ public enum StarType {
3844
RED_GIANT("red giant"),
3945
WHITE_DWARF("white dwarf"),
4046
SUPERNOVA("supernova"),
41-
DEAD("dead star"),
42-
UNDEFINED("");
43-
44-
private final String title;
45-
46-
StarType(String title) {
47-
this.title = title;
48-
}
49-
50-
@Override
51-
public String toString() {
52-
return title;
53-
}
47+
DEAD("dead star");
48+
...
5449
}
5550
```
5651

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.
5854

5955
```java
6056
public interface StarMemento {
@@ -123,29 +119,8 @@ public class Star {
123119
private int ageYears;
124120
private int massTons;
125121

126-
public StarType getType() {
127-
return type;
128-
}
129-
130-
public void setType(StarType type) {
131-
this.type = type;
132-
}
133-
134-
public int getAgeYears() {
135-
return ageYears;
136-
}
137-
138-
public void setAgeYears(int ageYears) {
139-
this.ageYears = ageYears;
140-
}
141-
142-
public int getMassTons() {
143-
return massTons;
144-
}
145-
146-
public void setMassTons(int massTons) {
147-
this.massTons = massTons;
148-
}
122+
// setters and getters ->
123+
...
149124
}
150125
}
151126
```
@@ -172,27 +147,33 @@ And finally here's how we use the mementos to store and restore star states.
172147
star.setMemento(states.pop());
173148
LOGGER.info(star.toString());
174149
}
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
185150
```
186151

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+
```
187165

188166
## Class diagram
167+
189168
![alt text](./etc/memento.png "Memento")
190169

191170
## Applicability
171+
192172
Use the Memento pattern when
193173

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
176+
object's encapsulation
196177

197178
## Real world examples
198179

memento/src/main/java/com/iluwatar/memento/StarType.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public enum StarType {
3131
RED_GIANT("red giant"),
3232
WHITE_DWARF("white dwarf"),
3333
SUPERNOVA("supernova"),
34-
DEAD("dead star"),
35-
UNDEFINED("");
34+
DEAD("dead star");
3635

3736
private final String title;
3837

0 commit comments

Comments
 (0)