Skip to main content

Memento Pattern in JavaScript (Live Playground)

The Memento pattern is a behavioral design pattern that allows you to capture an object's internal state without exposing its implementation details. This pattern enables you to restore an object to its previous state, which is particularly useful when implementing features like undo and redo. In this tutorial, we'll explore the Memento pattern in JavaScript, including its use cases, implementation, and benefits.

Why Use the Memento Pattern?

The Memento pattern is useful when you want to:

  1. Save and restore an object's internal state without violating its encapsulation.
  2. Implement undo and redo functionality.
  3. Provide a simple mechanism for taking snapshots of an object's state.

Implementing the Memento Pattern in JavaScript

Here's an example of how to implement the Memento pattern using JavaScript classes:

TypeScript
// Memento class
class Memento {
constructor(state) {
this.state = state;
}

getState() {
return this.state;
}
}

// Originator class
class Originator {
constructor() {
this.state = null;
}

setState(state) {
this.state = state;
}

getState() {
return this.state;
}

createMemento() {
return new Memento(this.state);
}

restore(memento) {
this.state = memento.getState();
}
}

// Caretaker class
class Caretaker {
constructor() {
this.mementos = [];
}

addMemento(memento) {
this.mementos.push(memento);
}

getMemento(index) {
return this.mementos[index];
}
}

// Client code
const originator = new Originator();
const caretaker = new Caretaker();

originator.setState('State1');
caretaker.addMemento(originator.createMemento());

originator.setState('State2');
caretaker.addMemento(originator.createMemento());

originator.restore(caretaker.getMemento(0));
console.log(originator.getState()); // Output: State1

In this example, the Memento class holds the saved state, the Originator class is responsible for creating mementos and restoring its state from them, and the Caretaker class manages a collection of mementos. The client code uses the caretaker to save and restore the originator's state.

Live Playground, Try it Yourself

Benefits of the Memento Pattern

Implementing the Memento pattern in your JavaScript projects offers several benefits:

  1. Encapsulation: The Memento pattern preserves an object's encapsulation by not exposing its internal state, which makes your code more robust and maintainable.
  2. Simplicity: The Memento pattern provides a straightforward mechanism for saving and restoring an object's state, making it easy to implement undo and redo functionality.
  3. Separation of Concerns: The Memento pattern separates the responsibility of saving and restoring an object's state from the object itself, promoting separation of concerns and code maintainability.

Conclusion

In summary, the Memento pattern is a valuable tool in JavaScript development that can help you create maintainable, decoupled code. By understanding and implementing this pattern, you can enhance your web development skills and create more robust applications.