Memento Design Pattern | C++ Design Patterns
Last Updated : 05 Jan, 2024
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 for Memento Design Pattern in C++
Example for Memento Design Pattern in C++:
Problem Statement:
Suppose we have to represent two states by the use of memento patterns then we can use three classes to represent this Originator class to store the data then memento patterns for storing the state of originator then caretaker will hold the memento to solve the problem.
Key Component of Memento Method:
The Memento pattern aims to capture an object's internal state, so it can be restored to that state later.
- Originator: This is the object whose state we want to capture. The Originator creates a Memento object to save its state and uses it to restore its state.
- Memento: This is an object that stores the state of the Originator. It has two primary interfaces, getState() returns the saved stateand setState(state) sets the state to a specific value.
- Caretaker: This is responsible for holding and managing Memento objects. It doesn't modify the Memento but can save and restore the state of the Originator.
Step wise Implementation of above Example:
So In the above example, we have an Originator class that has an internal state, which can be saved and restored using the Memento pattern. The Caretaker class is responsible for managing the Memento objects, allowing you to save and restore the Originator's state.
Originator: This is the object whose state we want to save and restore.
C++ class Originator { private: std::string state; public: void SetState(const std::string& newState) { state = newState; } std::string GetState() const { return state; } Memento SaveStateToMemento() { return Memento(state); } void RestoreStateFromMemento(const Memento& memento) { state = memento.GetState(); } };
Memento: This class represents the saved state of the Originator.
C++ class Memento { private: std::string state; public: Memento(const std::string& stateToSave) : state(stateToSave) {} std::string GetState() const { return state; } };
Caretaker: The Caretaker is responsible for storing and managing the Mementos.
C++ class Caretaker { private: std::vector<Memento> mementos; public: void AddMemento(const Memento& memento) { mementos.push_back(memento); } Memento GetMemento(int index) const { if (index >= 0 && index < mementos.size()) { return mementos[index]; } throw std::out_of_range("Invalid memento index"); } };
Then in the main class we use all these stored classes to get the output.
Overall Code for above Example:
C++ #include <iostream> #include <string> #include <vector> // Originator: The object whose state needs to be saved and restored. class Originator { private: std::string state; public: void SetState(const std::string& newState) { state = newState; } std::string GetState() const { return state; } // Memento: Inner class representing the state of the Originator. class Memento { private: std::string state; public: Memento(const std::string& originatorState) : state(originatorState) {} std::string GetSavedState() const { return state; } }; // Create a Memento object to save the current state. Memento CreateMemento() const { return Memento(state); } // Restore the state from a Memento object. void RestoreState(const Memento& memento) { state = memento.GetSavedState(); } }; // Caretaker: Manages the Memento objects. class Caretaker { private: std::vector<Originator::Memento> mementos; public: void AddMemento(const Originator::Memento& memento) { mementos.push_back(memento); } Originator::Memento GetMemento(int index) const { if (index >= 0 && index < mementos.size()) { return mementos[index]; } throw std::out_of_range("Invalid Memento index"); } }; int main() { Originator originator; Caretaker caretaker; originator.SetState("State 1"); caretaker.AddMemento(originator.CreateMemento()); originator.SetState("State 2"); caretaker.AddMemento(originator.CreateMemento()); // Restore to the previous state originator.RestoreState(caretaker.GetMemento(0)); std::cout << "Current state: " << originator.GetState() << std::endl; // Restore to an even earlier state originator.RestoreState(caretaker.GetMemento(1)); std::cout << "Current state: " << originator.GetState() << std::endl; return 0; }
OutputCurrent state: State 1 Current state: State 2
Diagrammatic Representation of Memento Method in C++:
Diagrammatic Representation of Momento Design Pattern in C++
In this example, we create an Originator object, change its state, and save the state into the Caretaker using the SaveStateToMemento method. Later, we can restore the state using the RestoreStateFromMemento method. The Caretaker is responsible for storing and managing the Mementos, allowing us to go back to previous states of the Originator.
Advantages of Memento Pattern in C++:
- Easy State Management: Managing an object's state history becomes straightforward with the Memento Pattern. It allows us to save and restore the object's state at different points in time without cluttering the object's interface or code.
- Encapsulation of Object State: The Memento Pattern encapsulates an object's state within a separate Memento object. This ensures that the object's state is not directly accessible from external classes, maintaining the principle of encapsulation and information hiding.
- Support for Undo and Redo: The Memento Pattern is widely used to implement undo and redo functionality in applications. By storing and managing a history of an object's states, we can easily revert the object to a previous state or move forward to a more recent state, providing a seamless user experience.
- Snapshot and Rollback: The pattern enables us to take snapshots of an object's state at specific moments, which can be valuable for debugging and analysis. we can also rollback an object to a previous state to troubleshoot or examine issues.
- Flexibility: The pattern is flexible and can be adapted to various use cases. we can implement it with different levels of granularity, from saving the entire object state to just specific parts of it.
- Maintains Object Integrity: By encapsulating the state management logic within the Memento, the Memento Pattern helps maintain the integrity of the object. It prevents accidental modification of the object's state outside of the intended mechanisms.
- Extensibility: We can extend the Memento Pattern to implement additional features. For example, we can add features like version control, branching, or sharing object states with other objects or users.
- Improved Testing and Debugging: With the ability to capture and restore object states, the Memento Pattern can aid in testing and debugging by allowing us to isolate specific states for testing or analysis.
The Memento Pattern is a valuable tool for managing and preserving the state of objects in a way that enhances code modularity, flexibility, and maintainability, making it particularly useful in applications where state management is critical.
Disadvantages of Memento pattern in C++:
- Increased Memory Usage: Storing the state of an object requires additional memory. This can become a concern if we are working with large objects or a large number of objects, as it may lead to increased memory usage.
- Performance Overhead: The process of creating and restoring states can introduce some performance overhead. This is particularly true if the state is complex or if there are frequent state changes, as the system needs to manage and store multiple states.
- Encapsulation Violation: In order to capture and restore the internal state of an object, the Memento pattern may require exposing the internal details of the object. This can violate the principles of encapsulation, making the object's implementation details accessible to external classes.
- Versioning and Compatibility: If the internal structure of the object changes, the Memento pattern may face challenges in terms of versioning and compatibility. This can be an issue when trying to restore the state of an object saved with an older version of the Memento.
- Complexity in Implementation: Implementing the Memento pattern may introduce additional complexity to the codebase. Managing the creation, storage, and retrieval of mementos can be intricate, especially in larger systems.
- Storage Management: Managing the storage of multiple mementos can become challenging, especially if there is a need to limit the number of stored states or if there are constraints on storage resources.
- Dependency on Originator: The Memento pattern creates a dependency between the originator (the object whose state is being managed) and the memento (the object representing the state). Changes in the originator may require corresponding changes in the memento, and this tight coupling can impact system flexibility.
Similar Reads
Modern C++ Design Patterns Tutorial
Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
7 min read
Creational Software Design Patterns in C++
Factory Method Pattern | C++ Design Patterns
Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the client code and the concrete products. Table of Content What is the Factory Method D
8 min read
Abstract Factory Pattern | C++ Design Patterns
Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are
6 min read
Builder Pattern | C++ Design Patterns
The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
6 min read
Prototype Pattern | C++ Design Patterns
When designing software, it's crucial to make it efficient, easy to reuse, and simple to maintain. One way to achieve these goals is by using design patterns, and one such pattern is the Prototype Pattern. In this article, we'll explore the Prototype Design Pattern in the context of C++. Important T
5 min read
Singleton Pattern | C++ Design Patterns
A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier. The Singleton pattern is useful when we need to have
11 min read
Structural Software Design Patterns in C++
Adapter Pattern | C++ Design Patterns
Adapter Pattern is a structural design pattern used to make two incompatible interfaces work together. It acts as a bridge between two incompatible interfaces, allowing them to collaborate without modifying their source code. This pattern is particularly useful when integrating legacy code or third-
6 min read
Bridge Method | C++ Design Patterns
Bridge Pattern is basically a structural design pattern in software engineering or in C++ programming that is used to separate an object's abstraction from its implementation. It is part of the Gang of Four (GoF) design patterns and is particularly useful when we need to avoid a permanent binding be
9 min read