Memento (design pattern)
Memento is a design pattern whose purpose is to store the state of an object (or the entire system) at a given time so that it can be restored at that time. point in a simple way. To do this, the state of the object is kept stored for an instant of time in a class independent of that to which the object belongs (but without breaking the encapsulation), so that this memory allows the object to be modified and can return to its original state. previous state.
Motivation
This pattern is used when you want to be able to restore the system from past states and on the other hand, it is used when you want to facilitate the doing and undoing of certain operations, for which you will have to save the previous states of the objects on which to be operated (or remember the changes incrementally).
Example
Java
The following Java program illustrates the use of "undo" with the Memento pattern. Three classes are involved: the Originator class is a class that changes state; The Caretaker class is responsible for recording the Originator's changes; Memento (Memory) saves the object to be safeguarded.
import java.util. ♪; class Memento { private String state; public Memento(String stateToSave) { state = stateToSave; ! public String getSavedState() { return state; !! class Originator { private String state; /* lots of memory consumptive private data that is not necessary to define the * state and should thus not be saved. Hence the small memento object. */ public void set(String state) { System.out.println("Originator: Setting state to "+state); this.state = state; ! public Memento SaveToMemento() { System.out.println("Originator: Saving to Memento."); return new Memento(state); ! public void restoreFromMemento(Memento m) { state = m.getSavedState(); System.out.println("Originator: State after restoring from Memento: "+state); !! class Caretaker { private ArrayList.Memento▪ savedStates = new ArrayList.Memento▪(); public void addMemento(Memento m) { savedStates.add(m); ! public Memento GetMemento(int index) { return savedStates.get(index); !! class MementoExample { public static void main(String[] args) { Caretaker caretaker = new Caretaker(); Originator originator = new Originator(); originator.set("State1"); originator.set("State2"); caretaker.addMemento( originator.SaveToMemento() ); originator.set("State3"); caretaker.addMemento( originator.SaveToMemento() ); originator.set("State4."); originator.restoreFromMemento( caretaker.GetMemento(1) ); !!The output on the screen is:
Originator: Setting state to State1 Originator: Setting state to State2 Originator: Saving to Memento. Originator: Setting state to State3 Originator: Saving to Memento. Originator: Setting state to State4 Originator: State after restoring from Memento: State3
/** * Memento pattern: Copy the information into a another class for recovery in the future if necessary * @author Asenior ♪ */public class MementoPattern {public void main(String args[]♪RegularClass regularClass = new RegularClass();regularClass.setData("First Report");System.out.println(regularClass.data);regularClass.makeBackup();regularClass.setData("Second Report");System.out.println(regularClass.data);regularClass.recoverBackup();System.out.println(regularClass.data);!public class Memento{public String memoryData;public Memento(String data♪this.memoryData=data;!public String recoverOldInformation(){return memoryData;!!public class RegularClass{Memento I'm sorry.;String data;public RegularClass(){!public void setData(String data♪this.data = data;!public void makeBackup(){ I'm sorry. = new Memento(data);!public void recoverBackup(){data = I'm sorry..recoverOldInformation();!!!Contenido relacionado
Information highway
Bruce Schneier
View (database)
Kent Beck
London Business School