Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Memento pattern

From Wikipedia, the free encyclopedia
Software design pattern

Thememento pattern is asoftware design pattern that exposes the private internal state of an object.One example of how this can be used is to restore an object to its previous state (undo via rollback), another is versioning, another is custom serialization.

The memento pattern is implemented with three objects: theoriginator, acaretaker and amemento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). 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.

Classic examples of the memento pattern include apseudorandom number generator (each consumer of the PRNG serves as a caretaker who can initialize the PRNG (the originator) with the same seed (the memento) to produce an identical sequence of pseudorandom numbers) and the state in a finite state machine.

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the Memento design pattern.[1]

In the above UML class diagram, theCaretaker class refers to theOriginator class for saving (createMemento()) and restoring (restore(memento)) originator's internal state.
TheOriginator class implements
(1)createMemento() by creating and returning aMemento object that stores originator's current internal stateand
(2)restore(memento) by restoring state from the passed inMemento object.

TheUML sequence diagramshows the run-time interactions:
(1) Saving originator's internal state: TheCaretaker object callscreateMemento() on theOriginator object,which creates aMemento object, saves its current internal state (setState()), and returns theMemento to theCaretaker.
(2) Restoring originator's internal state: TheCaretaker callsrestore(memento) on theOriginator object and specifies theMemento object that stores the state that should be restored. TheOriginator gets the state (getState()) from theMemento to set its own state.

Java example

[edit]

The following Java program illustrates the "undo" usage of the memento pattern.

importjava.util.List;importjava.util.ArrayList;classOriginator{privateStringstate;// The class could also contain additional data that is not part of the// state saved in the memento..publicvoidset(Stringstate){this.state=state;System.out.println("Originator: Setting state to "+state);}publicMementosaveToMemento(){System.out.println("Originator: Saving to Memento.");returnnewMemento(this.state);}publicvoidrestoreFromMemento(Mementomemento){this.state=memento.getSavedState();System.out.println("Originator: State after restoring from Memento: "+state);}publicstaticclassMemento{privatefinalStringstate;publicMemento(StringstateToSave){state=stateToSave;}// accessible by outer class onlyprivateStringgetSavedState(){returnstate;}}}classCaretaker{publicstaticvoidmain(String[]args){List<Originator.Memento>savedStates=newArrayList<Originator.Memento>();Originatororiginator=newOriginator();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.savedStates.add(originator.saveToMemento());originator.set("State4");originator.restoreFromMemento(savedStates.get(1));}}

The output is:

Originator: Setting state to State1Originator: Setting state to State2Originator: Saving to Memento.Originator: Setting state to State3Originator: Saving to Memento.Originator: Setting state to State4Originator: State after restoring from Memento: State3

This example uses a String as the state, which is an immutable object in Java. In real-life scenarios the state willalmost always be a mutable object, in which case a copy of the state must be made.

It must be said that the implementation shown has a drawback: it declares an internal class. It would be better if this memento strategy could apply to more than one originator.

There are mainly three other ways to achieve Memento:

  1. Serialization.
  2. A class declared in the same package.
  3. The object can also be accessed via a proxy, which can achieve any save/restore operation on the object.

C# example

[edit]

The memento pattern allows one to capture the internal state of an object without violating encapsulation such that later one can undo/revert the changes if required. Here one can see that thememento object is actually used torevert the changes made in the object.

classMemento{privatereadonlystringsavedState;privateMemento(stringstateToSave){savedState=stateToSave;}publicclassOriginator{privatestringstate;// The class could also contain additional data that is not part of the// state saved in the memento.publicvoidSet(stringstate){Console.WriteLine("Originator: Setting state to "+state);this.state=state;}publicMementoSaveToMemento(){Console.WriteLine("Originator: Saving to Memento.");returnnewMemento(state);}publicvoidRestoreFromMemento(Mementomemento){state=memento.savedState;Console.WriteLine("Originator: State after restoring from Memento: "+state);}}}classCaretaker{staticvoidMain(string[]args){varsavedStates=newList<Memento>();varoriginator=newMemento.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.savedStates.Add(originator.SaveToMemento());originator.Set("State4");originator.RestoreFromMemento(savedStates[1]);}}

Python example

[edit]
"""Memento pattern example."""classOriginator:_state=""defset(self,state:str)->None:print(f"Originator: Setting state to{state}")self._state=statedefsave_to_memento(self)->"Memento":returnself.Memento(self._state)defrestore_from_memento(self,m:"Memento")->None:self._state=m.get_saved_state()print(f"Originator: State after restoring from Memento:{self._state}")classMemento:def__init__(self,state):self._state=statedefget_saved_state(self):returnself._statesaved_states=[]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])

Javascript example

[edit]
// The Memento pattern is used to save and restore the state of an object.// A memento is a snapshot of an object's state.varMemento={// Namespace: MementosavedState:null,// The saved state of the object.save:function(state){// Save the state of an object.this.savedState=state;},restore:function(){// Restore the state of an object.returnthis.savedState;}};// The Originator is the object that creates the memento.// defines a method for saving the state inside a memento.varOriginator={// Namespace: Originatorstate:null,// The state to be stored// Creates a new originator with an initial state of nullcreateMemento:function(){return{state:this.state// The state is copied to the memento.};},setMemento:function(memento){// Sets the state of the originator from a mementothis.state=memento.state;}};// The Caretaker stores mementos of the objects and// provides operations to retrieve them.varCaretaker={// Namespace: Caretakermementos:[],// The mementos of the objects.addMemento:function(memento){// Add a memento to the collection.this.mementos.push(memento);},getMemento:function(index){// Get a memento from the collection.returnthis.mementos[index];}};varaction_step="Foo";// The action to be executed/the object state to be stored.varaction_step_2="Bar";// The action to be executed/the object state to be stored.// set the initial stateOriginator.state=action_step;Caretaker.addMemento(Originator.createMemento());// save the state to the historyconsole.log("Initial State: "+Originator.state);// Foo// change the stateOriginator.state=action_step_2;Caretaker.addMemento(Originator.createMemento());// save the state to the historyconsole.log("State After Change: "+Originator.state);// Bar// restore the first state - undoOriginator.setMemento(Caretaker.getMemento(0));console.log("State After Undo: "+Originator.state);// Foo// restore the second state - redoOriginator.setMemento(Caretaker.getMemento(1));console.log("State After Redo: "+Originator.state);// Bar

References

[edit]
  1. ^"The Memento design pattern - Structure and Collaboration".w3sDesign.com. Retrieved2017-08-12.

External links

[edit]
  • Description of Memento Pattern in Ada
  • Memento UML Class Diagram with C# and .NET code samples
  • SourceMaking Tutorial
  • Memento Design Pattern using Java
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Memento_pattern&oldid=1243079109"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp