Memento Pattern
The Memento Pattern (Memento Pattern) saves a certain state of an object so that the object can be restored when appropriate; the Memento Pattern belongs to behavioral patterns.
The Memento Pattern allows capturing and restoring an object's internal state without breaking encapsulation.
Introduction
Intent
Capture an object's internal state without breaking encapsulation, and allow these states to be saved and restored outside the object.
Main problem solved
- Allows capturing and saving an object's internal state so that it can be restored to that state in the future, enabling undo and rollback operations.
Use Cases
- When a mechanism needs to be provided to allow the user to revert to a previous state.
Implementation
- Create the memento class: Used to store and encapsulate the object's state.
- Create the originator role: Responsible for creating the memento and restoring the state as needed.
- Create the memento management class(optional): Responsible for managing all memento objects.
Key code
- Memento: Stores the originator's state information.
- Originator: Creates the memento and restores the state from the memento.
Application Example
- Regret pill: Provides the function of an undo operation.
- Game save: Saves game progress, allowing players to load previous saves.
- Ctrl+Z in Windows: Implements the undo operation.
- Back button in IE browser: Allows the user to go back to a previous page.
- Database transaction management: Saves state through transaction logs to implement rollback.
Advantages
- Provides a state restoration mechanism: Allows the user to conveniently return to a historical state.
- Encapsulates state information: The user does not need to care about the details of state saving.
Disadvantages
- Resource consumption: If the object's state is complex, saving the state may consume more resources.
Usage recommendation
- Use the Memento Pattern in scenarios where data state needs to be saved and restored.
- Consider using the Prototype Pattern combined with the Memento Pattern to save memory.
Notes
- To reduce coupling, memento objects should be managed indirectly through the memento management class.
- The Memento Pattern should be used with caution to avoid excessive consumption of system resources.
structures
The Memento Pattern contains the following main roles:
-
Memento (Memento): Responsible for storing the internal state of the originator object. A memento can hold part or all of the originator's state information.
-
Originator (Originator): Creates a memento object and can use the memento object to restore its own internal state. The originator usually creates a memento object when the state needs to be saved and uses the memento object when the state needs to be restored.
-
Caretaker (Caretaker): Responsible for saving the memento object, but does not operate on or inspect the memento object. The caretaker can only pass the memento to other objects.
Create a class that implements
The Memento Pattern uses three classes Memento、Originator and CareTaker. Memento contains the state of the object to be restored. Originator creates and stores the state in the Memento object. The Caretaker object is responsible for restoring the object's state from the Memento.
MementoPatternDemo, and our demo class uses the CareTaker and Originator object to display the restoration of the object's state.
Step 1
Create the Memento class.
Memento.java
public
class
Memento
{
private
String
state
;
public
Memento
(
String
state
)
{
this
.
state
=
state
;
}
public
String
getState
(
)
{
return
state
;
}
}
Step 2
Create the Originator class.
Originator.java
public
class
Originator
{
private
String
state
;
public
void
setState
(
String
state
)
{
this
.
state
=
state
;
}
public
String
getState
(
)
{
return
state
;
}
public
Memento
saveStateToMemento
(
)
{
return
new
Memento
(
state
)
;
}
public
void
getStateFromMemento
(
Memento
Memento
)
{
state
=
Memento
.
getState
(
)
;
}
}
Step 3
Create the CareTaker class.
CareTaker.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
CareTaker
{
private
List
<
Memento
mementoList
=
new
ArrayList
<
Memento
(
);
public
void
add
(
Memento
state
)
{
mementoList
.
add
(
state
)
;
}
public
Memento
get
(
int
index
)
{
return
mementoList
.
get
(
index
)
;
}
}
Step 4
Use CareTaker and Originator object.
MementoPatternDemo.java
public
class
MementoPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Originator
originator
=
new
Originator
(
)
;
CareTaker
careTaker
=
new
CareTaker
(
)
;
originator
.
setState
(
"
State #1
"
)
;
originator
.
setState
(
"
State #2
"
)
;
careTaker
.
add
(
originator
.
saveStateToMemento
(
)
)
;
originator
.
setState
(
"
State #3
"
)
;
careTaker
.
add
(
originator
.
saveStateToMemento
(
)
)
;
originator
.
setState
(
"
State #4
"
)
;
System
.
out
.
println
(
"
Current State:
"
originator
.
getState
(
)
)
;
originator
.
getStateFromMemento
(
careTaker
.
get
(
0
)
)
;
System
.
out
.
println
(
"
First saved State:
"
originator
.
getState
(
)
)
;
originator
.
getStateFromMemento
(
careTaker
.
get
(
1
)
)
;
System
.
out
.
println
(
"
Second saved State:
"
originator
.
getState
(
)
)
;
}
}
Step 5
Verify the output.
Current State: State #4
First saved State: State #2
Second saved State: State #3