Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Observer method - Python Design Patterns
Next article icon

Memento Method Design Pattern in Python

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Memento Design Pattern is a behavioral design pattern that allows you to capture and restore an object’s state without revealing its internal structure. This pattern is particularly useful in implementing undo mechanisms. In this article, we will explore the Memento Method Design Pattern in Python, providing practical examples and use cases to help you understand how to implement and utilize this pattern effectively.

Memento-Method-Design-Pattern-in-Python

Memento Method Design Pattern in Python

Important Topics for Memento Method Design Pattern in Python

  • What is the Memento Method Design Pattern?
  • Components of the Memento Method Design Pattern
  • Class Diagram of Memento Method Design Pattern
  • Example implementation of the Memento Method Design Pattern in Python
  • When to use Memento Method Design Pattern in Python?
  • When not to use Memento Method Design Pattern in Python?

What is the Memento Method Design Pattern?

The Memento Design Pattern is a behavioral design pattern used in software development to capture and externalize an object’s internal state so that it can be restored later without violating encapsulation. The Memento pattern is useful for implementing features like undo/redo operations, state persistence, and checkpoints in applications, allowing the system to revert to a previous state when needed.

Components of the Memento Method Design Pattern

The Memento Method Design Pattern comprises three main components:

1. Originator

  • The object whose internal state is to be saved and restored.
  • It creates a Memento containing a snapshot of its current state and can use the Memento to restore its state later.
  • Example: In a text editor, the Originator could be the document being edited.

2. Memento

  • The object that stores the internal state of the Originator.
  • It is a snapshot of the Originator’s state at a particular point in time.
  • Mementos are usually immutable to ensure the state cannot be modified once it is captured.
  • Example: In a text editor, the Memento could store the content of the document at a specific time.

3. Caretaker

  • The object responsible for managing the Memento.
  • It requests a Memento from the Originator, holds it, and passes it back to the Originator when needed for state restoration.
  • The Caretaker does not modify or inspect the contents of the Memento.
  • Example: In a text editor, the Caretaker could be the undo manager that keeps a stack of Mementos for undo/redo operations.

These components work together to provide a mechanism for saving and restoring an object’s state without exposing its internal details, thus maintaining encapsulation and ensuring state management is handled cleanly and efficiently.

Class Diagram of Memento Method Design Pattern

memento

Class Diagram of Memento Method Design Pattern

  • The caretaker calls the createMemento() method on the originator asking the originator to pass it a memento object.
  • At this point the originator creates a memento object saving its internal state and passes the memento to the caretaker.
  • The caretaker maintains the memento object and performs the operation. In case of the need to undo the operation, the caretaker calls the setMemento() method on the originator passing the maintained memento object.
  • The originator would accept the memento, using it to restore its previous state.

Example implementation of the Memento Method Design Pattern in Python

Problem Statement:

Consider a text editor application where users can type and edit text. The application needs to support undo and redo operations so that users can revert to previous versions of their text or redo actions they have undone. Implementing these features requires saving and restoring the state of the text document at different points in time.

How Memento Method Design Pattern helps to solve this problem?

The Memento Design Pattern can be used to capture the state of the text document (i.e., the text content) at different points in time, allowing users to undo or redo changes. The pattern involves:

  • The Originator (the text editor) which creates and restores Mementos.
  • The Memento which stores the state of the text.
  • The Caretaker which manages the Mementos (e.g., keeping track of undo and redo operations).
memento

Class Diagram of Memento Method Design Pattern for above example

Below is the implementation of the above solution:

1. Originator

The Originator class represents the text editor that can create and restore Mementos.

Python
class Originator:     def __init__(self, text):         self.text = text      def set_text(self, text):         self.text = text      def create_memento(self):         return Memento(self.text)      def restore_memento(self, memento):         self.text = memento.get_state()              def __str__(self):         return self.text 


2. Memento

The Memento class stores the state of the Originator.

Python
class Memento:     def __init__(self, state):         self._state = state      def get_state(self):         return self._state 


3. Caretaker

The Caretaker class manages the Mementos, allowing undo and redo operations.

Python
class Caretaker:     def __init__(self):         self._mementos = []         self._current_index = -1      def save(self, memento):         # Remove future states if any         if self._current_index + 1 < len(self._mementos):             self._mementos = self._mementos[:self._current_index + 1]          self._mementos.append(memento)         self._current_index += 1      def undo(self):         if self._current_index > 0:             self._current_index -= 1             return self._mementos[self._current_index]         return None      def redo(self):         if self._current_index + 1 < len(self._mementos):             self._current_index += 1             return self._mementos[self._current_index]         return None 


Below is the complete combined code discussed above:

Python
class Originator:     def __init__(self, text):         self.text = text      def set_text(self, text):         self.text = text      def create_memento(self):         return Memento(self.text)      def restore_memento(self, memento):         self.text = memento.get_state()              def __str__(self):         return self.text   class Memento:     def __init__(self, state):         self._state = state      def get_state(self):         return self._state   class Caretaker:     def __init__(self):         self._mementos = []         self._current_index = -1      def save(self, memento):         # Remove future states if any         if self._current_index + 1 < len(self._mementos):             self._mementos = self._mementos[:self._current_index + 1]          self._mementos.append(memento)         self._current_index += 1      def undo(self):         if self._current_index > 0:             self._current_index -= 1             return self._mementos[self._current_index]         return None      def redo(self):         if self._current_index + 1 < len(self._mementos):             self._current_index += 1             return self._mementos[self._current_index]         return None   # Example usage if __name__ == "__main__":     # Create an Originator (Text Editor)     editor = Originator("Initial Text")      # Create a Caretaker     caretaker = Caretaker()      # Save the initial state     caretaker.save(editor.create_memento())      # Make changes and save the state     editor.set_text("First Edit")     caretaker.save(editor.create_memento())      editor.set_text("Second Edit")     caretaker.save(editor.create_memento())      # Perform undo operations     print("Current State:", editor)  # Output: "Second Edit"     memento = caretaker.undo()     if memento:         editor.restore_memento(memento)     print("After Undo:", editor)  # Output: "First Edit"      # Perform redo operations     memento = caretaker.redo()     if memento:         editor.restore_memento(memento)     print("After Redo:", editor)  # Output: "Second Edit" 


Below is the explanation of the above code:

  • Originator:
    • The Originator class represents the text editor with methods to set text, create Mementos, and restore from Mementos.
    • It maintains the current state of the text and allows for the creation and restoration of Mementos.
  • Memento:
    • The Memento class holds a snapshot of the Originator’s state. It provides a method to retrieve the state but does not allow modifications.
  • Caretaker:
    • The Caretaker class manages a list of Mementos, enabling undo and redo functionality.
    • It saves Mementos, and can undo or redo changes by moving the index in the list of Mementos.

By using the Memento Design Pattern, the text editor application can efficiently manage state changes, allowing users to revert to previous text versions without exposing the internal details of the text management system.

When to use Memento Method Design Pattern in Python?

The Memento Design Pattern is particularly useful in scenarios where you need to capture and restore the state of an object without exposing its internal details. Here’s when you should consider using the Memento Pattern:

  1. Undo/Redo Functionality:
    • Use Case: When implementing features like undo and redo in applications such as text editors or graphic design tools.
    • Example: A text editor where users need to revert to previous versions of their document or redo actions that were undone.
  2. Snapshot Management:
    • Use Case: When you need to save and restore the state of an object at specific points in time, allowing for historical states to be revisited.
    • Example: A game where you want to save the state of the game (e.g., player position, scores) at various checkpoints.
  3. State Preservation Across Transactions:
    • Use Case: When you need to preserve the state of an object across multiple transactions or operations, ensuring that the state can be recovered if necessary.
    • Example: An online shopping cart that allows users to revert to a previous state if they navigate away from the cart and come back later.
  4. Encapsulation of State Management:
    • Use Case: When you want to encapsulate the state management of an object to avoid exposing its internal details and ensure that the state management is handled in a controlled manner.
    • Example: A drawing application where the state of the canvas (e.g., drawings, colors) needs to be managed without exposing the internal structure of the drawing objects.
  5. Temporary State Storage:
    • Use Case: When you need to temporarily store the state of an object for a specific duration and restore it later without affecting the main object’s state.
    • Example: A configuration management system where temporary configurations can be saved and restored as needed.

When not to use Memento Method Design Pattern in Python?

The Memento Method Design Pattern, while useful in many scenarios, is not always the best choice. Here are situations where you might want to avoid using the Memento Pattern:

  1. Simple State Management:
    • Reason: If the state of the object is simple and does not require frequent saving or restoration, the overhead of implementing the Memento Pattern might be unnecessary.
    • Example: A basic configuration object with a few properties that rarely changes.
  2. High Memory Usage:
    • Reason: The Memento Pattern involves storing multiple snapshots of an object’s state, which can lead to high memory consumption, especially if the object state is large or changes frequently.
    • Example: An application that needs to maintain a large number of states (e.g., a complex graphics editor with numerous large images).
  3. Frequent State Changes:
    • Reason: If the state of the object changes very frequently, managing and storing all those states can become impractical and inefficient.
    • Example: A real-time gaming system where the state changes rapidly and requires constant updates.
  4. No Need for State Restoration:
    • Reason: If there is no requirement for restoring previous states and you don’t need undo/redo functionality, the Memento Pattern may not be necessary.
    • Example: A system where only the current state is relevant, and historical states are not needed.
  5. Security Concerns:
    • Reason: If the state contains sensitive information, storing multiple snapshots of the state might pose security risks.
    • Example: An application that deals with sensitive user data where storing multiple states might expose private information.




Next Article
Observer method - Python Design Patterns
author
chaudhary_19
Improve
Article Tags :
  • Design Pattern
  • Python
  • System Design
  • python-design-pattern
Practice Tags :
  • python

Similar Reads

  • Python Design Patterns Tutorial
    Design patterns in Python are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best pra
    8 min read
  • Creational Software Design Patterns in Python

    • Factory Method - Python Design Patterns
      Factory Method is a Creational Design Pattern that allows an interface or a class to create an object, but lets subclasses decide which class or object to instantiate. Using the Factory method, we have the best ways to create an object. Here, objects are created without exposing the logic to the cli
      4 min read
    • Abstract Factory Method - Python Design Patterns
      Abstract Factory Method is a Creational Design pattern that allows you to produce the families of related objects without specifying their concrete classes. Using the abstract factory method, we have the easiest ways to produce a similar type of many objects. It provides a way to encapsulate a group
      4 min read
    • Builder Method - Python Design Patterns
      Builder Method is a Creation Design Pattern which aims to "Separate the construction of a complex object from its representation so that the same construction process can create different representations." It allows you to construct complex objects step by step. Here using the same construction code
      5 min read
    • Prototype Method Design Pattern in Python
      The Prototype Method Design Pattern in Python enables the creation of new objects by cloning existing ones, promoting efficient object creation and reducing overhead. This pattern is particularly useful when the cost of creating a new object is high and when an object's initial state or configuratio
      6 min read
    • Singleton Method - Python Design Patterns
      Prerequisite: Singleton Design pattern | Introduction What is Singleton Method in PythonSingleton Method is a type of Creational Design pattern and is one of the simplest design patterns available to us. It is a way to provide one and only one object of a particular type. It involves only one class
      5 min read
    • Structural Software Design Patterns in Python

      • Adapter Method - Python Design Patterns
        Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method is one of the easiest methods to understand because we have a lot of real-life examples that show the analogy with it. The main purpose of this method is to cre
        4 min read
      • Bridge Method - Python Design Patterns
        The bridge method is a Structural Design Pattern that allows us to separate the Implementation Specific Abstractions and Implementation Independent Abstractions from each other and can be developed considering as single entities.The bridge Method is always considered as one of the best methods to or
        5 min read
      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences