What is Memento?
Memento is a behavioral design pattern that is used mostly for saving the state of an object at a specific time. These states are snapshots of an object.
Memento object stores and provides states of a target object, which we can use in cases like undo/redo some operations.
We can implement this pattern on objects that can be embedded or external objects.
Google Bard
Q: Show me real-life examples of Memento design pattern
A:
Here are some real-life examples of the Memento design pattern:
Undo/redo in text editors:
Text editors such as Microsoft Word and Google Docs use the Memento pattern to implement undo/redo functionality. When a user changes a document, the text editor creates a Memento object that contains a snapshot of the document's state. When the user clicks the undo button, the text editor restores the document to the state represented by the Memento object.
Version control systems:
Version control systems such as Git and Mercurial use the Memento pattern to store and manage the history of a project's files. When a developer commits changes to a file, the version control system creates a Memento object that contains a snapshot of the file's contents. The Memento objects are stored in a repository, and developers can use them to restore the files to any previous state.
Checkpoints in video games:
Video games often use the Memento pattern to implement checkpoints. When a player reaches a checkpoint, the game creates a Memento object that contains a snapshot of the player's progress. If the player dies or loses the game, they can restart from the last checkpoint.
Transaction logs in databases:
Transaction logs in databases use the Memento pattern to implement rollbacks. When a transaction is started, the database creates a Memento object that contains a snapshot of the database's state. If the transaction fails, the database can use the Memento object to roll back the changes made by the transaction.
