Open In App

Memento Method – Python Design Patterns

Last Updated : 06 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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!
 

Memento-Problem-diagram

Memento-Problem-diagram


 

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.
 

Python3
"""Memento class for saving the data"""

class Memento:

    """Constructor function"""
    def __init__(self, file, content):

        """put all your file content here"""
        
        self.file = file
        self.content = content

"""It's a File Writing Utility"""

class FileWriterUtility:

    """Constructor Function"""

    def __init__(self, file):

        """store the input file data"""
        self.file = file
        self.content = ""

    """Write the data into the file"""

    def write(self, string):
        self.content += string

    """save the data into the Memento"""

    def save(self):
        return Memento(self.file, self.content)

    """UNDO feature provided"""

    def undo(self, memento):
        self.file = memento.file
        self.content = memento.content

"""CareTaker for FileWriter"""

class FileWriterCaretaker:

    """saves the data"""

    def save(self, writer):
        self.obj = writer.save()

    """undo the content"""

    def undo(self, writer):
        writer.undo(self.obj)


if __name__ == '__main__':

    """create the caretaker object"""
    caretaker = FileWriterCaretaker()

    """create the writer object"""
    writer = FileWriterUtility("GFG.txt")

    """write data into file using writer object"""
    writer.write("First vision of GeeksforGeeks\n")
    print(writer.content + "\n\n")

    """save the file"""
    caretaker.save(writer)

    """again write using the writer """
    writer.write("Second vision of GeeksforGeeks\n")

    print(writer.content + "\n\n")

    """undo the file"""
    caretaker.undo(writer)

    print(writer.content + "\n\n")

UML Diagram

Following is the UML diagram for Memento’s Method
 

Memento-method-UML-Diagram

Memento-method-UML-Diagram


 

Advantages


 

  • Encourages Encapsulation: Memento method can help in producing the state of the object without breaking the encapsulation of the client’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.


 

Disadvantages


 

  • 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 languages, 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 the result.


 

Applicability


 

  • 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
 



Previous Article
Next Article

Similar Reads

Memento Design Pattern | C++ Design Patterns
Memento Design Pattern is a behavioral design pattern that provides a mechanism for capturing an object's internal state and restoring it to that state at a later time. This pattern is useful when we need to implement features like undo/redo functionality or when we want to save and restore an object's state for various reasons. Important Topics fo
7 min read
Memento Method - JavaScript Design Pattern
A behavioral design pattern in JavaScript called Memento focuses on externalizing and capturing an object's internal state so that it can later be restored. When you need to add features like undo/redo functionality, history tracking, or reverting an item to a former state, this pattern is quite helpful. Important Topics for the Memento MethodUnder
6 min read
Memento Design Pattern
The Memento design pattern is a behavioral design pattern used to capture and restore the internal state of an object without exposing its implementation details. Important Topics for the Memento Design Pattern What is the Memento Design Pattern?Components of Memento Design PatternCommunication between the componentsReal-World Analogy of Memento De
8 min read
Difference Between Architectural Style, Architectural Patterns and Design Patterns
Many software professionals think that architectural styles and patterns are the same. Sadly, some of the software developers don’t understand the difference between architectural patterns and design patterns. In this article, we're going to summarize the differences between them. According to MSDN, architectural styles and patterns are the same th
7 min read
Connectivity and Composition Patterns | Design Patterns for Cloud Native Applications
In cloud-native applications, the architectural design becomes a critical aspect, defining the success of the entire solution. Connectivity and composition patterns are fundamental design principles that dictate how different components within a cloud-native application communicate and collaborate. Let's delve deeper into the significance of these
9 min read
Strategy Method Design Pattern | C++ Design Patterns
Strategy Pattern is a behavioral design pattern that defines a family of interchangeable algorithms and allows them to be used interchangeably within a context. This pattern enables the algorithm to be selected at runtime, providing flexibility and promoting code reusability. Important Topics for the Strategy Method in C++ Design Patterns Example o
4 min read
State Method Design Pattern | C++ Design Patterns
In software design, managing the behavior of an object according to its internal state is a common issue. The state pattern addresses this issue by allowing an object to alter its behavior every time its internal state changes. This pattern encapsulates each state in a separate class, which makes it easier to add new states and modify existing stat
7 min read
Template Method Design Pattern | C++ Design Patterns
Template Method Pattern introduces a template in a superclass that defines the steps of an algorithm. These steps may include both common tasks shared among subclasses and specific tasks that need customization. Subclasses then implement or override these steps to modify the algorithm according to their specific needs. Important Topics for Template
7 min read
Composite Design Pattern | JavaScript Design Patterns
The Composite Design pattern is a way of organizing objects. It helps us handle different objects in a similar way when they are put together to create a structure with parts and wholes. These parts and wholes are like building blocks that can be split into smaller pieces and then put together to make a tree-like structure. The composite design pat
6 min read
What is the Correlation Between System Design and Design Patterns?
System design and design patterns are closely related concepts in software engineering, with design patterns serving as reusable solutions to common design problems encountered during system design. System design and design patterns are interrelated concepts that work together to create robust and well-structured software systems. Important Topics
11 min read
three90RightbarBannerImg