Memento Pattern is one of the behavioral patterns. Memento pattern is a software design pattern that provides the ability to restore state of an object to its previous state.

The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is an object that has an internal state. The caretaker asks the originator for a memento object. Then caretaker does something which make changes to the originator, but has the ability to undo the change. To roll back to the state before operations, it returns the memento object to the originator.

For more information please go to http://en.wikipedia.org/wiki/Memento_pattern

Example

The real life example would be when we write something to the notepad, wordpad etc. then we can undo the text or characters using ctrl+z in Windows Operating System.

Implementation

Create a Memento class which will track the state

package com.roytuts.designpattern.memento;

public class Memento {

  private String state;

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

  public String getState() {
    return state;
  }

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

}

Next create a class which will save to and restore from Memento

package com.roytuts.designpattern.memento;

public class Originator {

  private String state;

  public void set(String state) {
    System.out.println("Originator: Setting time to " + state);
    this.state = state;
  }

  public Memento saveToMemento() {
    System.out.println("Originator: Saving to Memento.");
    return new Memento(state);
  }

  public void restoreFromMemento(Memento memento) {
    if (memento instanceof Memento) {
      Memento mem = (Memento) memento;
      this.state = mem.getState();
      System.out.println("Originator: Time restored from Memento: " + state);
    }
  }

}

Now create a class to add objects to and get from Memento

package com.roytuts.designpattern.memento;

import java.util.ArrayList;
import java.util.List;

public class CareTaker {

  private List<Object> objects = new ArrayList<Object>();

  public void addToMemento(Object obj) {
    objects.add(obj);
  }

  public Object getFromMemento(int index) {
    return objects.get(index);
  }

}

Create a test class for testing the pattern

package com.roytuts.designpattern.memento;

public class MementoPatternTest {

  /**
   * @param args
   */
  public static void main(String[] args) {

    Originator originator = new Originator();
    CareTaker careTaker = new CareTaker();

    originator.set("A year ago");
    careTaker.addToMemento(originator.saveToMemento());
    originator.set("6 months ago");
    careTaker.addToMemento(originator.saveToMemento());
    originator.set("7 days ago");
    originator.restoreFromMemento((Memento) careTaker.getFromMemento(1));

  }

}

Output

Originator: Setting time to A year ago
Originator: Saving to Memento.
Originator: Setting time to 6 months ago
Originator: Saving to Memento.
Originator: Setting time to 7 days ago
Originator: Time restored from Memento: 6 months ago

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *