Application Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Memento Design Pattern Example

8/28/2015

0 Comments

 
The word 'memento' refers to an object kept as a reminder or souvenir of a person or event. Memento pattern is a behavioral pattern in GoF because it captures and externalize an object's internal state so that the object can be restored to this state at a later time.
Picture
source: GoF

overview

Memento, the class that represents a snapshot, simply contains the state of an object to be restored to an Originator. You and get and set state that a Memento encapsulates inside.

Originator, the class in which the current state is held. Originator has a member function that creates and return a Memento with the current state of the Originator stored in the returned Memento. Originator also has a member function that sets its current state with the state of a given Memento object. In other words, it can create snapshot (Memento), or restore the state to the given snapshot (Memento).

Caretaker is the helper class that is responsible for storing and restoring the Originator’s state through Memento object. A Caretaker object keeps and holds the Mementos, but never modify the Mementos. Caretaker class is responsible for the memento's safekeeping. For example, a Caretaker encapsulates a list of Mementos that you can add more to the list, or get a Memento from the list.

memento code example

Memento class:
public class Memento {
  private String state;

  public Memento(String state){
    this.state = state;
  }

  public String getState(){
    return state;
  }
}
Originator class:
public class Originator {
   private String state;

   public void setState(String state){
      this.state = state;
   }

   public String getState(){
      return state;
   }

   public Memento createMemento(){           // return a current snapshot
      return new Memento(state);
   }

   public void setMemento(Memento Memento){  // restore from a snapshot
      state = Memento.getState();
   }
}
Caretaker class:
public class Caretaker {
   private List<Memento> mementoList = new ArrayList<Memento>();

   public void add(Memento state){
      mementoList.add(state);
   }

   public Memento get(int index){
      return mementoList.get(index);
   }
}
main():
public class MementoPatternDemo {
   public static void main(String[] args) {
   
      Originator originator = new Originator();
      Caretaker caretaker = new Caretaker();
      
      originator.setState("State #1");
      originator.setState("State #2");
      caretaker.add(originator.createMemento());
      
      originator.setState("State #3");
      caretaker.add(originator.createMemento());
      
      originator.setState("State #4");
      System.out.println("Current State: " + originator.getState());
      
      originator.setState(caretaker.get(0));
      System.out.println("First saved State: " + originator.getState());
      originator.setState(caretaker.get(1));
      System.out.println("Second saved State: " + originator.getState());
   }
}
Output:
Current State: State #4
First saved State: State #2
Second saved State: State #3

when to use memento

  • Need an undo and restore mechanism.
  • Database rollback. To be able to rollback, have the Originator create a snapshot (memento) before performing an operation and thus in case of failure, the memento object would be used to restore the Originator to its previous state.
  • Want to simplify Originator code so it does not need to keep track of its previous state by moving such responsibility to the Caretaker code.

comparison

memento vs command
  • Both can act as magic tokens to be passed around and invoked at a later time.
  • ​In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time.
  • Both can support muti-level undos.
  • Memento pattern's capturing and restoring object state is more expensive then Command patterns for Undo/Redo functionality.

references

Memento Pattern
Multilevel Undo and Redo Implementation in C# - Part III (Using Memento Pattern)

0 Comments



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar