Can someone give me some real world examples for Memento Design Pattern. I heard that Cart can be considered as one of the Memento Design Patterns. That kind of examples are preferred. Thanks in advance. :)
1 Answers
Undo/Redo functions
According to Refactoring Guru,
Problem
Imagine that you’re creating a text editor app. In addition to simple text editing, your editor can format text, insert inline images, etc.At some point, you decided to let users undo any operations carried out on the text. This feature has become so common over the years that nowadays people expect every app to have it. For the implementation, you chose to take the direct approach. Before performing any operation, the app records the state of all objects and saves it in some storage. Later, when a user decides to revert an action, the app fetches the latest snapshot from the history and uses it to restore the state of all objects.
Applicability
Use the Memento pattern when you want to produce snapshots of the object’s state to be able to restore a previous state of the object.The Memento pattern lets you make full copies of an object’s state, including private fields, and store them separately from the object. While most people remember this pattern thanks to the “undo” use case, it’s also indispensable when dealing with transactions (i.e., if you need to roll back an operation on error).

- 325
- 8
- 26