The Memento pattern is used to capture and externalize an object's internal state so that it can be restored later without violating encapsulation. This is useful for implementing features like undo/redo functionality.
class Memento {
constructor(state) {
this.state = state;
}
getState() {
return this.state;
}
}
// Originator
class Originator {
constructor() {
this.state = "";
}
setState(state) {
this.state = state;
console.log(`State set to: ${this.state}`);
}
saveStateToMemento() {
return new Memento(this.state);
}
getStateFromMemento(memento) {
this.state = memento.getState();
console.log(`State restored to: ${this.state}`);
}
}
// Caretaker
class Caretaker {
constructor() {
this.mementoList = [];
}
add(memento) {
this.mementoList.push(memento);
}
get(index) {
return this.mementoList[index];
}
}Classes and Methods
- Memento
- Purpose: Stores the state of the
Originator. - Constructor: Takes a
stateparameter and assigns it to the instance. getState(): Returns the stored state.
-
Originator
- Purpose: Creates and stores states in
Mementoobjects. - Constructor: Initializes the
stateto an empty string. setState(state): Sets the current state and logs it.saveStateToMemento(): Creates a newMementowith the current state.getStateFromMemento(memento): Restores the state from a givenMementoand logs it.
- Purpose: Creates and stores states in
-
Caretaker
- Purpose: Manages the
Mementoobjects. - Constructor: Initializes an empty list to store
Mementoobjects. - add(memento): Adds a
Mementoto the list. - get(index): Retrieves a
Mementofrom the list by index.
- Purpose: Manages the
const originator = new Originator();
const 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");
console.log("Current State:", originator.state);
originator.getStateFromMemento(caretaker.get(0));
console.log("First saved State:", originator.state);
originator.getStateFromMemento(caretaker.get(1));
console.log("Second saved State:", originator.state);- Create instances of
OriginatorandCaretaker. - Set various states in the
Originator. - Save states to
Mementoobjects and store them in theCaretaker. - Restore states from
Mementoobjects using theCaretaker.
The Originator class can save its state to a Memento object and restore it later. The Caretaker class manages these Memento objects, allowing the Originator to revert to previous states. The Memento pattern is useful for scenarios where you need to implement undo/redo functionality.