Memento Method is a Behavioral Design pattern which provides the ability to restore an object to its previous state. Without revealing the details of concrete implementations, it allows you to save and restore the previous version of the object. It tries not to disturb the encapsulation of the code and allows you to capture and externalize an object’s internal state.
Problem without using Memento Method
Imagine you are a student who wants to excel in the world of competitive programming but you are facing one problem i.e., finding a nice Code Editor for programming but none of the present code editors fulfills your needs so you trying to make one for yourself. One of the most important features of any Code Editor is UNDO and REDO which essentially you also need. As an inexperienced developer, you just used the direct approach of storing all the performed actions. Of course, this method will work but Inefficiently!
Solution by Memento Method
Let’s discuss the solution to the above-discussed problem. The whole problem can be easily resolved by not disturbing the encapsulation of the code. The problem arises when some objects try to perform extra tasks that are not assigned to them, and due to which they invade the private space of other objects. The Memento pattern represents creating the state snapshots to the actual owner of that state, the originator object. Hence, instead of other objects trying to copy the editor’s state from the “outside, ” the editor class itself can make the snapshot since it has full access to its own state.
According to the pattern, we should store the copy of the object’s state in a special object called Memento and the content of the memento objects are not accessible to any other object except the one that produced it.
Following is the UML diagram for Memento’s Method
- Encourages Encapsulation: Memento method can help in producing the state of the object without breaking the encapsulation of the clien’s code.
- Simplifies Code: We can take the advantage of caretaker who can help us in simplifying the code by maintaining the history of the originator’s code.
- Generic Memento’s Implementation: It’s better to use Serialization to achieve memento pattern implementation that is more generic rather than Memento pattern where every object needs to have it’s own Memento class implementation.
- Huge Memory Consumption: If the Originator’s object is very huge then Memento object size will also be huge and use a lot of memory which is definitely not the efficient way to do the work.
- Problem with Dynamic Languages: Programming languages like Ruby, Python, and PHP are dynamically typed langauges, can’t give the guarantee that the memento object will not be touched.
- Difficult Deletion: It’s not easy to delete the memento object because the caretaker has to track the originator’s lifecycle inorder to get th result.
- UNDO and REDO:Most of the software applications like Paint, Coding IDEs, text editor, and many others provide UNDO and REDO features for the ease of client.
- Providing Encapsulation: We can use the Memento’s method for avoiding the breakage of encapsulation in the client’s code which might be produced by direct access to the object’s internal implementation.
Further Read – Memento Method in Java
- Factory Method - Python Design Patterns
- Observer method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Abstract Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Singleton Method - Python Design Patterns
- Adapter Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- Iterator Method - Python Design Patterns
- Visitor Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Template Method - Python Design Patterns
- State Method - Python Design Patterns
- Command Method - Python Design Patterns
- Chain of Responsibility - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.