Memento pattern
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
Classic examples of the memento pattern include the seed of a pseudorandom number generator (it will always produce the same sequence thereafter when initialized with the seed state)[citation needed][clarification needed] and the state in a finite state machine. —Wikipedia
简而言之,快照。跟Command模式有些相似。Command模式是撤销。
Structure
In the above UML class diagram, the Caretaker
class refers to the Originator
class for saving (createMemento()
) and restoring (restore(memento)
) originator’s internal state.
The Originator
class implements
(1) createMemento()
by creating and returning a Memento
object that stores originator’s current internal state and
(2) restore(memento)
by restoring state from the passed in Memento
object.
The UML sequence diagram shows the run-time interactions:
(1) Saving originator’s internal state: The Caretaker
object calls createMemento()
on the Originator
object, which creates a Memento
object, saves its current internal state (setState()
), and returns the Memento
to the Caretaker
.
(2) Restoring originator’s internal state: The Caretaker
calls restore(memento)
on the Originator
object and specifies the Memento
object that stores the state that should be restored. The Originator
gets the state (getState()
) from the Memento
to set its own state.
Example
import java.util.List;
import java.util.ArrayList;
class Originator {
private String state;
// The class could also contain additional data that is not part of the
// state saved in the memento..
public void set(String state) {
this.state = state;
System.out.println("Originator: Setting state to " + state);
}
public Memento saveToMemento() {
System.out.println("Originator: Saving to Memento.");
return new Memento(this.state);
}
public void restoreFromMemento(Memento memento) {
this.state = memento.getSavedState();
System.out.println("Originator: State after restoring from Memento: " + state);
}
public static class Memento {
private final String state;
public Memento(String stateToSave) {
state = stateToSave;
}
// accessible by outer class only
private String getSavedState() {
return state;
}
}
}
class Caretaker {
public static void main(String[] args) {
List<Originator.Memento> savedStates = new ArrayList<Originator.Memento>();
Originator originator = new Originator();
originator.set("State1");
originator.set("State2");
savedStates.add(originator.saveToMemento());
originator.set("State3");
// We can request multiple mementos, and choose which one to roll back to.
savedStates.add(originator.saveToMemento());
originator.set("State4");
originator.restoreFromMemento(savedStates.get(0));
}
}
Summary
- 记录状态,便于回滚
- 一般用于快照,保存记录