[KongchangAI]
ConceptMemento Pattern

Memento Pattern

A software design pattern that enables undo and rollback functionality by saving a snapshot of an object's state at a specific point in time. The pattern defines three classic roles: Originator, Memento, and Caretaker. It was formalized in the 1994 Gang of Four design patterns book.

Timeline (last 90 days)

Oct 9

The core idea of the Memento Pattern is to take a snapshot of the current state before making a change, and to restore the object directly to that snapshot state when an undo is needed.

Unverified50%
Oct 9

The Memento Pattern has three classic roles: the Originator, the Memento, and the Caretaker.

Unverified50%
Oct 9

An object that owns state should be responsible for snapshotting and restoring itself; the mechanism that manages history does not need to know how the object works internally, only that it can snapshot and restore.

Unverified50%
Oct 9

The book "Design Patterns" catalogues 23 classic design patterns, of which the Memento Pattern is one.

Unverified50%
Oct 9

The fundamental difference between the Memento Pattern and the Command Pattern is: the Memento stores state, while the Command stores operations.

Unverified50%
Oct 9

The Memento is suitable when the cost of copying state is low, there are many different types of mutations, and restoring old state is simpler than reversing each operation; the Command Pattern is often better when snapshots are large, individual changes are small, or the operations themselves are meaningful domain concepts that need to enter an audit log.

Unverified50%
Oct 9

Restoring an object's state is not the same as undoing side effects; it is impossible to unsend a sent email, reverse an API call, or refund money to someone else's account — external systems require more complex architectural mechanisms such as transactions and compensating operations.

Unverified50%
Oct 9

A snapshot should not automatically serialize every attribute, but should only capture state relevant to the purpose of restoration; runtime information such as last_run_at and execution counts should generally not be rolled back during an undo.

Unverified50%
Oct 9

Adding redo functionality only requires maintaining two stacks in History — undo and redo: when performing an undo, push the snapshot onto the redo stack; when performing a redo, push it back onto the undo stack.

Unverified50%

All Facts (9)

Unverified

The core idea of the Memento Pattern is to take a snapshot of the current state before making a change, and to restore the object directly to that snapshot state when an undo is needed.

50%
Unverified

The Memento Pattern has three classic roles: the Originator, the Memento, and the Caretaker.

50%
Unverified

An object that owns state should be responsible for snapshotting and restoring itself; the mechanism that manages history does not need to know how the object works internally, only that it can snapshot and restore.

50%
Unverified

The book "Design Patterns" catalogues 23 classic design patterns, of which the Memento Pattern is one.

50%
Unverified

The fundamental difference between the Memento Pattern and the Command Pattern is: the Memento stores state, while the Command stores operations.

50%
Unverified

The Memento is suitable when the cost of copying state is low, there are many different types of mutations, and restoring old state is simpler than reversing each operation; the Command Pattern is often better when snapshots are large, individual changes are small, or the operations themselves are meaningful domain concepts that need to enter an audit log.

50%
Unverified

Restoring an object's state is not the same as undoing side effects; it is impossible to unsend a sent email, reverse an API call, or refund money to someone else's account — external systems require more complex architectural mechanisms such as transactions and compensating operations.

50%
Unverified

A snapshot should not automatically serialize every attribute, but should only capture state relevant to the purpose of restoration; runtime information such as last_run_at and execution counts should generally not be rolled back during an undo.

50%
Unverified

Adding redo functionality only requires maintaining two stacks in History — undo and redo: when performing an undo, push the snapshot onto the redo stack; when performing a redo, push it back onto the undo stack.

50%

Source Articles