Latest web development tutorials

Memento Pattern

Memo mode (Memento Pattern) to save a state of an object in order to restore the object at the appropriate time. Memorandum patterns belong behavioral patterns.

Introduction

Intent: Without violating encapsulation of capturing the internal state of an object, and save the state outside of the object.

Mainly to solve: the model is the so-called memorandum without destroying the package, capture internal state of an object, and save the state outside of the object so that the object can later be restored to a previously saved state.

When to use: many times we always need to record the internal state of an object, the purpose of doing so is to allow users to cancel uncertain or incorrect operation, can be restored to his original state so that he has "regret" to eat.

How to fix: a memo by class designed to store the object state.

Key Code: customers do not couple with memo class, coupled with the memo management class.

Application examples: 1, regret it later.2, when playing the game archive. 3, Windows Lane ctri + z. 4, IE in the back. 4, the transaction management database.

Advantages: 1, to provide users with a way to restore the state of the mechanism that allows users to be more convenient to return to a history of the state.2, to achieve a package of information, so that the user does not need to be concerned about the state of preservation of the details.

Disadvantages: consume resources.If the member variables of the class too, is bound to take up a relatively large resources, and each time you save will consume a certain amount of memory.

Usage scenarios: 1, the need to save / restore the relevant state of the scene data.2, there is provided a rollback operation.

Note: 1, in order to meet the principles of Demeter, also add a class management memo.2, in order to save memory, you can use the prototype model + memo mode.

achieve

Memo mode uses three classesMemento,Originator andCareTaker.Memento containing the object to be restored in the state. Originator Memento object is created and stored in the state. Caretaker object is responsible for restoring objects from Memento state.

MementoPatternDemo,our demonstration classes usingCareTakerandOriginatorobject to display the state of the object recovery.

Memorandum pattern UML diagram

step 1

Create a Memento class.

Memento.java

public class Memento {
   private String state;

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

   public String getState () {
      return state;
   }	
}

Step 2

Create Originator class.

Originator.java

public class Originator {
   private String state;

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

   public String getState () {
      return state;
   }

   public Memento saveStateToMemento () {
      return new Memento (state);
   }

   public void getStateFromMemento (Memento Memento) {
      state = Memento.getState ();
   }
}

Step 3

Create CareTaker class.

CareTaker.java

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

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);
   }
}

Step 4

UseCareTakerandOriginatorobjects.

MementoPatternDemo.java

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.saveStateToMemento ());
      originator.setState ( "State # 3");
      careTaker.add (originator.saveStateToMemento ());
      originator.setState ( "State # 4");

      System.out.println ( "Current State:" + originator.getState ());		
      originator.getStateFromMemento (careTaker.get (0));
      System.out.println ( "First saved State:" + originator.getState ());
      originator.getStateFromMemento (careTaker.get (1));
      System.out.println ( "Second saved State:" + originator.getState ());
   }
}

Step 5

Verify output.

Current State: State # 4
First saved State: State # 2
Second saved State: State # 3