Memento Design Pattern Last Updated : 18 Dec, 2024 Comments Improve Suggest changes 7 Likes Like Report The Memento Design Pattern is a behavioral pattern that helps save and restore an object's state without exposing its internal details. It is like a "snapshot" that allows you to roll back changes if something goes wrong. It is widely used in undo-redo functionality in applications like text editors or games.Table of ContentWhat is the Memento Design Pattern?Components of Memento Design PatternCommunication between the componentsMemento Design Pattern Example(with implementation)When to use Memento Design Pattern?When not to use Memento Design Pattern?What is the Memento Design Pattern?Without violating encapsulation, the internal state of an object can be captured and restored using the Memento design pattern. It gives you the power to undo or roll back changes you've made to an object by saving and restoring its state to a prior one. You might wish to save checkpoints in your program as it develops so you can return to them at a later time.Components of Memento Design PatternOriginator:The state of an object is established and maintained by this component.It can create Memento objects to store its state and has methods to set and retrieve the object's state.The Originator communicates directly with the Memento to create snapshots of its state and to restore its state from a snapshot.Memento:The Memento is an object that stores the state of the Originator at a particular point in time. It only provides a way to retrieve the state, without allowing direct modification. This ensures that the state remains Caretaker:The Caretaker is responsible for keeping track of Memento objects. It doesn't know the details of the state stored in the Memento but can request Mementos from the Originator to save or restore the object's state.Client:Typically represented as the part of the application or system that interacts with the Originator and Caretaker to achieve specific functionality. The client initiates requests to save or restore the state of the Originator through the Caretaker.Communication between the componentsClient: The client initiates the process by requesting the Originator to perform some operation that may modify its state or require the state to be saved. For example, the client might trigger an action like "save state" or "restore state."Originator: The Originator either produces a Memento to save its current state (if the request is to save state) or retrieves a Memento to restore its prior state (if the request is to restore state).Caretaker: The Caretaker acts as an intermediary between the client and the Originator, managing the Memento objects.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.Memento Design Pattern Example(with implementation)Problem Statement:Consider creating a text editor application and want to include an undo feature that lets revert changes made to a document. The difficulty is in storing the document's state at different times and restoring it when required without disclosing the document's internal implementation.Benefits of Using Memento Pattern in this scenario:Encapsulation: By using the Memento pattern, you can prohibit direct access to and manipulation of the document's state by encapsulating it within Memento objects.Undo Functionality: The Memento pattern makes it possible to construct an undo feature that lets users undo changes and recover earlier document states by saving snapshots of the document's state at various points in time.Separation of Concerns: By separating state management responsibilities from the document itself, the Memento design encourages code that is clearer and easier to maintain.Below is the implementation of the above problemLet’s break down into the component wise code:1. Originator (Document) Java public class Document { private String content; public Document(String content) { this.content = content; } public void write(String text) { this.content += text; } public String getContent() { return this.content; } public DocumentMemento createMemento() { return new DocumentMemento(this.content); } public void restoreFromMemento(DocumentMemento memento) { this.content = memento.getSavedContent(); } } 2. Memento Java public class DocumentMemento { private String content; public DocumentMemento(String content) { this.content = content; } public String getSavedContent() { return this.content; } } 3. Caretaker (History) Java import java.util.ArrayList; import java.util.List; public class History { private List<DocumentMemento> mementos; public History() { this.mementos = new ArrayList<>(); } public void addMemento(DocumentMemento memento) { this.mementos.add(memento); } public DocumentMemento getMemento(int index) { return this.mementos.get(index); } } Complete code for the above exampleBelow is the complete code for the above example: Java import java.util.ArrayList; import java.util.List; // Originator class Document { private String content; public Document(String content) { this.content = content; } public void write(String text) { this.content += text; } public String getContent() { return this.content; } public DocumentMemento createMemento() { return new DocumentMemento(this.content); } public void restoreFromMemento(DocumentMemento memento) { this.content = memento.getSavedContent(); } } // Memento class DocumentMemento { private String content; public DocumentMemento(String content) { this.content = content; } public String getSavedContent() { return this.content; } } // Caretaker class History { private List<DocumentMemento> mementos; public History() { this.mementos = new ArrayList<>(); } public void addMemento(DocumentMemento memento) { this.mementos.add(memento); } public DocumentMemento getMemento(int index) { return this.mementos.get(index); } } public class Main { public static void main(String[] args) { Document document = new Document("Initial content\n"); History history = new History(); // Write some content document.write("Additional content\n"); history.addMemento(document.createMemento()); // Write more content document.write("More content\n"); history.addMemento(document.createMemento()); // Restore to previous state document.restoreFromMemento(history.getMemento(1)); // Print document content System.out.println(document.getContent()); } } OutputInitial content Additional content More contentWhen to use Memento Design Pattern?Undo functionality: When your application needs to include an undo function that lets users restore the state of an object after making modifications.Snapshotting: When you need to enable features like versioning or checkpoints by saving an object's state at different times.Transaction rollback: When there are failures or exceptions, like in database transactions, and you need to reverse changes made to an object's state.Caching: When you wish to reduce duplicate calculations or enhance efficiency by caching an object's state.When not to use Memento Design Pattern?Large object state: Keeping and managing several snapshots of an object's state might use a lot of memory and computing power if its state is big or complicated.Frequent state changes: It may become impractical or wasteful to save and manage snapshots of an object's state if its state changes regularly and unexpectedly.Immutable objects: The Memento pattern may not be very useful for capturing and restoring an object's state if it is easily reconstructable or immutable.Overhead: The codebase may become more complex if the Memento pattern is included, particularly if the application does not need capabilities like state rollback or undo functionality. Create Quiz Comment S saketkumr 7 Improve S saketkumr 7 Improve Article Tags : Misc Design Pattern System Design Explore What is System DesignSystem Design Introduction - LLD & HLD7 min readSystem Design Life Cycle | SDLC (Design)7 min readWhat are the components of System Design?10 min readGoals and Objectives of System Design5 min readWhy is it Important to Learn System Design?6 min readImportant Key Concepts and Terminologies â Learn System Design9 min readAdvantages of System Design4 min readSystem Design FundamentalsAnalysis of Monolithic and Distributed Systems - Learn System Design10 min readRequirements Gathering in System Design6 min readDifferences between System Analysis and System Design4 min readHorizontal and Vertical Scaling | System Design5 min readCapacity Estimation in Systems Design10 min readHow to Answer a System Design Interview Problem/Question?5 min readFunctional and Non Functional Requirements5 min readWeb Server, Proxies and their role in Designing Systems9 min readScalability in System DesignWhat is Scalability and How to achieve it?7 min readWhich Scalability approach is right for our Application? - System Design4 min readPrimary Bottlenecks that Hurt the Scalability of an Application - System Design4 min readDatabases in Designing SystemsComplete Guide to Database Design - System Design11 min readSQL vs. NoSQL - Which Database to Choose in System Design?5 min readFile and Database Storage Systems in System Design4 min readBlock, Object, and File Storage in System Design6 min readDatabase Sharding - System Design8 min readDatabase Replication in System Design6 min readHigh Level Design(HLD)What is High Level Design? - Learn System Design9 min readAvailability in System Design5 min readConsistency in System Design8 min readReliability in System Design5 min readCAP Theorem in System Design5 min readWhat is API Gateway?7 min readWhat is Content Delivery Network(CDN) in System Design7 min readWhat is Load Balancer & How Load Balancing works?4 min readCaching - System Design Concept8 min readCommunication Protocols in System Design6 min readActivity Diagrams - Unified Modeling Language (UML)10 min readMessage Queues - System Design12 min readLow Level Design(LLD)What is Low Level Design or LLD?6 min readAuthentication vs Authorization in LLD - System Design3 min readPerformance Optimization Techniques for System Design3 min readObject-Oriented Analysis and Design(OOAD)6 min readData Structures and Algorithms for System Design6 min readContainerization Architecture in System Design10 min readModularity and Interfaces In System Design8 min readUnified Modeling Language (UML) Diagrams8 min readData Partitioning Techniques in System Design5 min readHow to Prepare for Low-Level Design Interviews?4 min readEssential Security Measures in System Design8 min readDesign PatternsDesign Patterns Tutorial9 min readCreational Design Patterns4 min readStructural Design Patterns7 min readBehavioral Design Patterns5 min readDesign Patterns Cheat Sheet - When to Use Which Design Pattern?7 min readInterview Guide for System DesignHow to Crack System Design Interview Round?9 min readSystem Design Interview Questions and Answers1 min read5 Common System Design Concepts for Interview Preparation12 min read5 Tips to Crack Low-Level System Design Interviews6 min readSystem Design Interview Questions & AnswersMost Commonly Asked System Design Interview Questions3 min readDesign Dropbox - A System Design Interview Question14 min readDesigning Twitter - A System Design Interview Question15+ min readSystem Design Netflix | A Complete Architecture15+ min readSystem Design of Uber App | Uber System Architecture13 min readDesign BookMyShow - A System Design Interview Question10 min readDesigning Facebook Messenger | System Design Interview9 min readComplete Roadmap to Learn System Design for Beginners6 min readGuide to System Design for Freshers15+ min readHow Disney+ Hotstar Managed (5 Cr)+ Live Viewers During India's T20 World Cup Win[2024]8 min read Like