Memento Method Design Pattern: Saving and Restoring Object States
May 26, 2026 6 Min Read 44 Views
(Last Updated)
Imagine working on a document editor where users can undo changes anytime they want. A player stops and resumes the game at the same checkpoint. A photo editing software can roll back the changes made to the older version of an image if there are any changes made by mistake.
All these systems rely on one important concept: saving and restoring previous states.
In software development, it can be difficult to effectively manage the states of objects, especially in large applications, without leaking out the internal data. This is where the Memento Method Design Pattern becomes useful.
The Memento Method Design Pattern is a behavioral design pattern that allows applications to capture and restore an object’s previous state without violating encapsulation. The pattern does not expose the internal data directly, but rather keeps a record of snapshots of the object that can be restored when necessary.
In this blog, we are going to learn what is Memento Method Design Pattern, the components of it, implementation in real world scenarios, pros and cons of it, and simple coding examples to understand this design pattern.
Quick Answer
The Memento Method Design Pattern is a behavioral design pattern used to save and restore an object’s previous state without exposing internal implementation details. It is commonly used in undo systems, game save checkpoints, transaction rollbacks, and version history management to provide safe and organized state recovery.
Table of contents
- What is Memento Method Design Pattern?
- Why Developers Use the Memento Pattern
- Example
- Components of Memento Method Design Pattern
- Originator
- Memento
- Caretaker
- Implementation Example
- Main Class
- Output
- Understanding the Workflow
- Real-Life Software Applications
- Undo/Redo Systems
- Game Save Systems
- Database Transaction Rollbacks
- Version Control Systems
- Advantages of Memento Method Design Pattern
- Preserves Encapsulation
- Simplifies Undo Functionality
- Improves State Recovery
- Makes Code More Maintainable
- Disadvantages of Memento Method Design Pattern
- Increased Memory Usage
- Performance Overhead
- Difficult State Management for Large Objects
- Memento Pattern vs Command Pattern
- Memento Pattern
- Command Pattern
- When Should You Use the Memento Pattern?
- When Should You Avoid It?
- Wrapping it up:
- FAQs
- What is the Memento Method Design Pattern?
- Where is the Memento Pattern used?
- What are the components of Memento Pattern?
- What is the biggest advantage of the Memento Pattern?
- What is the difference between Memento and Command Pattern?
What is Memento Method Design Pattern?
The Memento Method Design Pattern is a behavioural design pattern that allows to save and restore the state of an object without revealing anything about its internal structure.
The idea of the pattern is to make a snapshot of the current state of an object and save it outside of the program. Later, the application can return the object to that saved state, if necessary.
It’s similar to a “Save Game” in video games.
- A game stores:
- Player position
- Health status
- Inventory
- Mission progress
Later, the player will be able to load the saved progress without having to start over.
The saved snapshot acts like a Memento.
Why Developers Use the Memento Pattern
Applications constantly change object states during execution.
If there is no state management:
- Undo functionality is hard to implement.
- Rollbacks become risky
- Data recovery becomes complicated
- Applications become more and more difficult to maintain.
The Memento Pattern addresses this issue by providing a safe way to save snapshots of an object’s states.
This pattern is used by developers because it:
- Preserves encapsulation
- Simplifies undo operations
- Improves state management
- Keeps object restoration clean and organized
Example
An easy to understand example in the real world is a text editor.
Assume that users type:
| Hello World |
Then changes it to:
| Hello Developers |
When the user selects Undo the editor will restore the previous state.
The application saves snapshots of the text object that can be used to track all character changes without having to manually do so.
This is exactly how the Memento Pattern works.
Applications like Microsoft Word and Adobe Photoshop use similar concepts for undo and version history features.
Components of Memento Method Design Pattern
The Memento Pattern mainly consists of three important components.
1. Originator
The Originator is the main object whose state needs to be saved and restored.
It generates memento objects for snapshots of its current state.
The Originator also has the ability to “roll back” itself to a previous state with the use of saved mementos.
Originator Code
| class TextEditor { private String content; public void setContent(String content) { this.content = content; } public String getContent() { return content; } // Save state public EditorMemento save() { return new EditorMemento(content); } // Restore state public void restore(EditorMemento memento) { content = memento.getSavedContent(); } } |
Explanation
In this class:
- content is used to store the text of the editor.
- save() takes a snapshot of the state.
- The restore() method is used to restore the saved state from the memento object.
Originator maintains its own state management with security.
2. Memento
The Memento stores the snapshot of the Originator’s state.
It serves as a copy that can be retrieved later on.
Memento Code
| class EditorMemento { private final String content; public EditorMemento(String content) { this.content = content; } public String getSavedContent() { return content; } } |
Explanation
This class saves content the editor has saved.
Important points:
- The state remains protected.
- Data cannot be modified directly by external classes.
- The memento simply acts as a storage container.
3. Caretaker
The Caretaker is responsible for the care and storage of mementos.
Does not directly change the states of objects.
Its job is simply to:
- Save snapshots
- Maintain history
- Save states if necessary.
Caretaker Code
| import java.util.Stack; class History { private Stack<EditorMemento> history = new Stack<>(); public void save(EditorMemento memento) { history.push(memento); } public EditorMemento undo() { return history.pop(); } } |
Explanation
The Caretaker maintains several snapshots, in a stack.
This enables applications to feature such things as:
- Undo operations
- State rollback
- History tracking
Implementation Example
So, let’s put all this together now.
Main Class
| public class Main { public static void main(String[] args){ TextEditor editor = new TextEditor(); History history = new History(); editor.setContent(“Version 1”); history.save(editor.save()); editor.setContent(“Version 2”); history.save(editor.save()); editor.setContent(“Version 3”); System.out.println( “Current Content: “ + editor.getContent() ); editor.restore(history.undo()); System.out.println( “After Undo: “ + editor.getContent() ); } } |
Output
| Current Content: Version 3 After Undo: Version 2 |
Understanding the Workflow
In this case, the following occurred:
- The editor’s current content was stored by the Originator and the process of saving and restoring object states was managed by the Originator as needed.
- The Memento stored snapshots of the editor at various points, acting like backup copies that could be restored later.
- The Caretaker maintained the saved history using a stack, allowing the application to track multiple previous states efficiently.
- The application loaded the last saved snapshot and reverted the editor to the saved version when the Undo operation was issued.
This way, the state management remains clean and tidy, and doesn’t reveal any of the implementation details of the object directly to the other classes.
Real-Life Software Applications
Memento Pattern is commonly used in applications that need to recover or rollback from states or manage history.
Undo/Redo Systems
Applications like text editors, drawing tools, and design software allow users to undo or redo actions by restoring previously saved object states.
For example:
- Microsoft Word recalls the material that has been previously typed.
- Previous image changes are restored in Adobe Photoshop.
- Figma keeps a design history and change tracking.
The Memento Pattern is used to restore previous states, without having to re-create all the changes, in these kinds of applications.
Game Save Systems
The Memento Pattern is often implemented in video games for saving player progress.
The game can keep data including:
- Health status
- Inventory items
- Mission checkpoints
- Character location
- Achievement progress
This enables players to do so without having to restart the whole game.
Database Transaction Rollbacks
Rollback systems are typically used in banking and enterprise applications to roll back transactions if they fail.
For example:
- Failed money transfers
- Payment processing errors
- Inventory update failures
The Memento Pattern ensures the reliability of a system by allowing the system to return to safe states in the past and avoid data corruption.
Version Control Systems
Applications that maintain version history often use concepts similar to the Memento Pattern.
These systems keeps snapshots of previous versions so that Users can:
- Restore old files
- Recover deleted changes
- Compare earlier versions
This is particularly helpful for editing documents and working on team collaboration platforms.
Advantages of Memento Method Design Pattern
The Memento Pattern offers a number of significant advantages in software development.
Preserves Encapsulation
One of the biggest advantages of this pattern is that object states remain protected.
By keeping the application architecture cleaner and increasing security, external classes won’t be able to access or alter the data of internal objects directly.
Simplifies Undo Functionality
Implementing undo and rollback operations is much simpler due to the pattern; applications can simply roll back to a previously saved snapshot, rather than re-construct old states manually.
Improves State Recovery
The Memento Pattern is a way to store previous states in a clean and structured manner when a failure, crash or incorrect operation occurs.
This increases the reliability and stabilization of complex systems.
Makes Code More Maintainable
State management becomes separated from business logic, making the application easier to organize, maintain, and debug over time.
Retrieval-Augmented Generation (RAG) was formally introduced in a 2020 paper by Lewis et al. from Facebook AI Research. It gained major traction in 2023 as enterprises recognized that fine-tuning alone was often insufficient for injecting or updating proprietary or rapidly changing knowledge inside large language models. RAG addresses this by combining information retrieval with language model generation, allowing systems to ground responses in external data sources. By 2026, it has become a dominant architectural pattern in production AI systems, widely adopted for building knowledge-aware applications that scale across large enterprise datasets.
Disadvantages of Memento Method Design Pattern
Although the pattern has its benefits, it also comes with its drawbacks.
Increased Memory Usage
When you take multiple snapshots of objects, you can use a lot of memory, particularly in applications that have complex objects or that change states often.
In large systems, this can be cost expensive.
Performance Overhead
If applications continuously save states after every small change, performance may decrease due to constant snapshot creation and storage operations.
Difficult State Management for Large Objects
Snapshots for very large objects containing complex data types can be tricky to manage and inefficiently optimize.
Memento Pattern vs Command Pattern
Developers sometimes confuse the Memento Pattern with the Command Pattern because both are commonly used in undo systems.
They are, however, different and do different things.
Memento Pattern
- Saves complete state of objects as snapshots.
- Rolls back changes when necessary.
- Primarily deals with state recovery and history management.
This pattern is best used when it’s necessary to restore the previous state of an object.
Command Pattern
- Stores actions or commands instead of object states.
- Undoes previous actions.
- Emphasizes executing and managing actions.
For example, instead of restoring a previous snapshot, the application may reverse a delete operation by executing an opposite command.
The difference between the two is that the Memento Pattern is for restoring the state of an object that has been saved, while the Command Pattern is for undoing the action or operation.
When Should You Use the Memento Pattern?
You should consider using the Memento Pattern when:
- Applications must have undo / redo / roll back capabilities.
- Object states should be restored safely while hiding the implementation details.
- Preserving encapsulation and protecting object data is important.
- History tracking and/or version management systems are maintained in applications.
This pattern is particularly suitable when using an editor, a gaming system, transaction processing applications, or enterprise software.
When Should You Avoid It?
The Memento Pattern should not be used when:
- States in objects are very large or memory intensive.
- Optimization of memory is an essential requirement.
- There is too much complexity and difficulty to state tracking, or to manage it efficiently.
- Applications go through state changes many, many times, which can impact performance.
In such a case, other solutions might be more scalable and optimized for performance.
Crack the tech world with HCL GUVI’s IITM Pravartak & MongoDB Certified AI Software Development Course, where coding meets real industry skills. From Java and DSA to System Design and AI-powered development, learn everything through practical projects, expert mentorship, and hands-on training designed to make you placement-ready from day one.own pace.
Wrapping it up:
The Memento Method Design Pattern is a powerful solution for applications that need reliable state management and recovery features. By storing object snapshots separately, it allows developers to implement functionalities like undo operations, rollback systems, and version history without exposing internal object details.
One of the key benefits of the Memento pattern is that it provides an organized approach to application development and simplifies the state recovery process in applications. The Memento pattern is found in many different types of applications; from text editors and gaming systems, through banking systems and large enterprise applications to many other types of software applications. When using the Memento pattern, developers will be able to build stable, user-friendly applications.
For students, learning this pattern improves understanding of object state management and behavioral design patterns. For professionals, it provides a clean and structured approach to handling recovery-based features in scalable software systems.
Hope you found this blog useful and gained a better understanding of how the Memento Method Design Pattern works in real-world software development.
FAQs
1. What is the Memento Method Design Pattern?
The Memento Method Design Pattern is a design pattern that is used to store and restore the state of an object without revealing the internal details of the object.
2. Where is the Memento Pattern used?
It is generally applied to undo systems, game saves, transaction rollbacks, and version control.
3. What are the components of Memento Pattern?
These are the key elements of the main:
Originator
Memento
Caretaker
4. What is the biggest advantage of the Memento Pattern?
It has the best advantage in maintaining the encapsulation and supporting the state restoration.
5. What is the difference between Memento and Command Pattern?
The Command Pattern is used for undoing actions or operations, the Memento Pattern is used for restoring saved states.



Did you enjoy this article?