Memento pattern

A Memento (English memento pattern, even token) is a design pattern in software development and belongs to the category of behavior (english behavioral patterns ). The pattern is used to detect and externalization of the internal state of an object, thereby ensuring that his or her encapsulation is not violated. The object can be restored at a later point in time in this state. It is one of the so-called GoF patterns.

Use

The Memento is used when

  • A snapshot of the (partial) state of an object to be cached
  • Should be prevented that a direct interface for determining the state implementation details reveals

A typical application example is the implementation of breakpoints or undo mechanisms.

Benefits

  • Data encapsulation can be maintained, thus no direct visibility or ability to access attributes of an object is created
  • The pattern provides a simple way of creating a partial interface

Example

The following Java program represents the undo mechanism ( undo) in Java

Public class MementoDemo {      public static void main ( String [ ] args ) {      Originator originator originator = new ();        originator.set ( " State1 ");      originator.set ( " State2 ");      Memento memento1 = originator.saveToMemento ();      originator.set ( " State3 ");      / / We can request multiple mementos, and choose which one to roll back to.      Memento memento2 = originator.saveToMemento ();      originator.set ( " STATE4 ");        originator.restoreFromMemento ( memento2 );    } }   public class Memento {    / ** State of the memento * /    private final String state;      public Memento ( final String stateToSave ) {      state = stateToSave;    }      public String getSavedState () {      return state;    } }   public class Originator {      / ** Current state * /    private String state;      / / The class Could so additional containment data did is not part of the    / / State saved in the memento.      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 memento ) {      state = memento.getSavedState ();      System.out.println ( "Originator: State after restoring from Memento :" state);    }   } The output 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 In this example, a string is used as a state, the default is immutable in Java. Usually the condition is but a normal object that has to be clone before it is used in Memento:

Private Memento (final State state) {      / / The state must be cloned first before      / / The memento is returned, or sequential      / / Views would access one and the same object.      mementoState = state.clone (); } The above implementation has a disadvantage in that it declares an internal class. It would be better if the Memento strategy would be applicable to more than one object.

There are basically three different ways to implement a memento:

563636
de