Memento pattern
The memento pattern is a software design pattern in the field of object-oriented programming that allows reverting the state of an object.[src] Uses of this design pattern include undo, version control, and serialization.[src] The memento pattern is implemented with three objects: the originator, a caretaker and a memento.[src] The originator is some object that has an internal state.[src] The caretaker is going to do something to the originator, but wants to be able to easily bring back the prior state.[src] The caretaker first asks the originator for a memento object.[src] Then it does whatever operation (or sequence of operations) it was going to do.[src] To roll back to the state before the operations, it returns the memento object to the originator.[src] The memento object itself is immutable.[src] When using this pattern, care should be taken if the originator may change other objects or resources—the memento pattern operates on a single object.[src] One classic example of this pattern is the pseudorandom number generator (PRNG).[src] In this case, each consumer of the PRNG serves as a caretaker who can initialize the PRNG (the originator) with a particular seed (the memento) to produce an identical sequence of pseudorandom numbers.[src]
The memento pattern is a software design pattern in the field of object-oriented programming that allows reverting the state of an object.[src] Uses of this design pattern include undo, version control, and serialization.[src] The memento pattern is implemented with three objects: the originator, a caretaker and a memento.[src] The originator is some object that has an internal state.[src] The caretaker is going to do something to the originator, but wants to be able to easily bring back the prior state.[src] The caretaker first asks the originator for a memento object.[src] Then it does whatever operation (or sequence of operations) it was going to do.[src] To roll back to the state before the operations, it returns the memento object to the originator.[src] The memento object itself is immutable.[src] When using this pattern, care should be taken if the originator may change other objects or resources—the memento pattern operates on a single object.[src] One classic example of this pattern is the pseudorandom number generator (PRNG).[src] In this case, each consumer of the PRNG serves as a caretaker who can initialize the PRNG (the originator) with a particular seed (the memento) to produce an identical sequence of pseudorandom numbers.[src] package org.wikipedia.examples; import java.util.ArrayList; import java.util.List; class Originator { private String state; // The class could also contain additional data that is not part of the // state saved in the memento..[src] public void set(String state) { this.state = state; System.out.printf("Originator: Setting state to %s%n", state); } public Memento saveToMemento() { System.out.println("Originator: Saving to Memento."); return new Memento(this.state); } public void restoreFromMemento(Memento memento) { this.state = memento.getSavedState(); System.out.printf("Originator: State after restoring from Memento: %s%n", state); } public static class Memento { private final String state; public Memento(String stateToSave) { state = stateToSave; } // accessible by outer class only private String getSavedState() { return state; } } } class Caretaker { public static void main(String[] args) { List savedStates = new ArrayList (); Originator originator = new Originator(); originator.set("State1"); originator.set("State2"); savedStates.add(originator.saveToMemento()); originator.set("State3"); // We can request multiple mementos, and choose which one to roll back to.[src] savedStates.add(originator.saveToMemento()); originator.set("State4"); originator.restoreFromMemento(savedStates.get(1)); } } The output is: Originator: Setting state to State1 Originator: Setting state to State2 Originator: Saving to Memento.[src] Originator: Setting state to State3 Originator: Saving to Memento.[src] Originator: Setting state to State4 Originator: State after restoring from Memento: State3 This example uses a String as the state, which is an immutable object in Java.[src] In real-life scenarios the state will almost always be a mutable object, in which case a copy of the state must be made.[src] It must be said that the implementation shown has a drawback: it declares an internal class.[src] It would be better if this memento strategy could apply to more than one originator.[src] There are mainly three other ways to achieve Memento: Serialization.[src] A class declared in the same package.[src] The object can also be accessed via a proxy, which can achieve any save/restore operation on the object.[src] namespace Wikipedia.Examples; class Memento { private readonly string _savedState; private Memento(string stateToSave) { _savedState = stateToSave; } public class Originator { private string _state; // The class could also contain additional data that is not part of the // state saved in the memento.[src] public void Set(string state) { Console.WriteLine(f"Originator: Setting state to {state}"); _state = state; } public Memento SaveToMemento() { Console.WriteLine("Originator: Saving to Memento."); return new Memento(_state); } public void RestoreFromMemento(Memento memento) { _state = memento.savedState; Console.WriteLine(f"Originator: State after restoring from Memento: {_state}"); } } } class Caretaker { static void Main(string[] args) { List savedStates = new(); Memento.Originator originator = new(); originator.Set("State1"); originator.Set("State2"); savedStates.Add(originator.SaveToMemento()); originator.Set("State3"); // We can request multiple mementos, and choose which one to roll back to.[src] savedStates.Add(originator.SaveToMemento()); originator.Set("State4"); originator.RestoreFromMemento(savedStates[1]); } } ==Python example== """ Memento pattern example.[src] """ class Originator: state: str = "" def set(self, state: str) -> None: print(f"Originator: Setting state to {state}") self.state = state def save_to_memento(self) -> "Memento": return self.Memento(self.state) def restore_from_memento(self, memento: "Memento") -> None: self.state = memento.get_saved_state() print(f"Originator: State after restoring from Memento: {self.state}") class Memento: state: str def __init__(self, state: str) -> None: self.state = state def get_saved_state(self) -> str: return self.state saved_states: list[Memento] = [] originator: Originator = Originator() originator.set("State1") originator.set("State2") saved_states.append(originator.save_to_memento()) originator.set("State3") saved_states.append(originator.save_to_memento()) originator.set("State4") originator.restore_from_memento(saved_states[1]) // The Memento pattern is used to save and restore the state of an object.[src] // A memento is a snapshot of an object's state.[src] let Memento = {// Namespace: Memento savedState : null, // The saved state of the object.[src] save : function(state) { // Save the state of an object.[src] this.savedState = state; }, restore : function() { // Restore the state of an object.[src] return this.savedState; } }; // The Originator is the object that creates the memento.[src] // defines a method for saving the state inside a memento.[src] let Originator = {// Namespace: Originator state : null, // The state to be stored // Creates a new originator with an initial state of null createMemento : function() { return { state : this.state // The state is copied to the memento.[src] }; }, setMemento : function(memento) { // Sets the state of the originator from a memento this.state = memento.state; } }; // The Caretaker stores mementos of the objects and // provides operations to retrieve them.[src] let Caretaker = {// Namespace: Caretaker mementos : [], // The mementos of the objects.[src] addMemento : function(memento) { // Add a memento to the collection.[src] this.mementos.push(memento); }, getMemento : function(index) { // Get a memento from the collection.[src] return this.mementos[index]; } }; let action_step = "Foo"; // The action to be executed/the object state to be stored.[src] let action_step_2 = "Bar"; // The action to be executed/the object state to be stored.[src] // set the initial state Originator.state = action_step; Caretaker.addMemento(Originator.createMemento());// save the state to the history console.log("Initial State: " + Originator.state); // Foo // change the state Originator.state = action_step_2; Caretaker.addMemento(Originator.createMemento()); // save the state to the history console.log("State After Change: " + Originator.state); // Bar // restore the first state - undo Originator.setMemento(Caretaker.getMemento(0)); console.log("State After Undo: " + Originator.state); // Foo // restore the second state - redo Originator.setMemento(Caretaker.getMemento(1)); console.log("State After Redo: " + Originator.state); // Bar ==External links== Description of Memento Pattern in Ada Memento UML Class Diagram with C# and .NET code samples SourceMaking Tutorial Memento Design Pattern using Java[src]
53 sentences · 53 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Memento pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/memento-pattern. Accessed 26 November 2026.
Chicago
"Memento pattern." encyclopegia.org. Accessed November 26, 2026. https://encyclopegia.org/wiki/memento-pattern.
APA
encyclopegia.org. (2026). Memento pattern. Retrieved November 26, 2026, from https://encyclopegia.org/wiki/memento-pattern
Accessed November 26, 2026 · CC BY-SA 4.0 · citation page · JSON