-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMementoTest.java
More file actions
52 lines (47 loc) · 1.26 KB
/
MementoTest.java
File metadata and controls
52 lines (47 loc) · 1.26 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
package tests.behavioural;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import patterns.behavioural.memento.Logbook;
/**
* MEMENTO PATTERN
*
* <p>Purpose: Captures and restores an object's internal state without violating encapsulation.
*
* <p>When to use:
* <ul>
* <li>When you need to implement undo/redo functionality</li>
* <li>When direct interface to getting state would expose implementation details</li>
* </ul>
*
* <p>Common Pitfalls:
* <ul>
* <li>Memory consumption with many mementos</li>
* <li>Not properly encapsulating state</li>
* </ul>
*/
class MementoTest {
/**
* Our mission requires a logbook to keep track of all the important information.
*
* <p>Use the Memento pattern to implement the {@link Logbook} class.
*/
@Test
void example() {
Logbook logbook = new Logbook();
logbook.write("One");
logbook.write("Two");
logbook.undo();
assertThat(logbook.getLatest()).isEqualTo("One");
}
@Test
@Disabled
void todo() {
/*
* todo:
* expand the Logbook support a redo functionality
* use it to restore the last undone entry
* try undoing in an empty logbook
* */
}
}