Memento Pattern

Memento Pattern

Memento Pattern (Memento Pattern), also known as Snapshot Pattern (Snapshop Pattern), is a behavioral pattern.

What is the memo pattern?

The memento pattern (Memento Pattern) saves a certain state of an object so that the object can be restored at an appropriate time. (regret drug mode)

UML

Role

  • Originator role: responsible for organizing the whole process of the memorandum, and backing up and restoring data by creating and importing memos.
  • Memento role: responsible for storing the internal state of the initiating role object, and preventing objects other than the corresponding initiating role from accessing the memento. The memo has two interfaces, and the manager can only see the narrow interface of the memo, and it can only pass the memo to other objects. The initiator is able to see a wide interface allowing it to access all the data needed to return to the previous state.
  • Manager (Caretaker): responsible for saving the Memento, and cannot operate or check the content of the Memento.

application

use template
public class MementoPattern {
    public static void main(String[] args) {
        //Create a manager to manage saved memos
        Caretaker caretaker = new Caretaker();
        //Create initiator
        Originator originator = new Originator();
        //Set the status of the initiator
        originator.setState("A");
        originator.print();
        System.out.println("Revise");
        caretaker.setMemento(originator.createMemento());
        originator.setState("B");
        originator.print();
        System.out.println("revoke");
        //perform undo
        originator.restoreMemento(caretaker.getMemento());
        originator.print();
    }
}

/
//Initiator
class Originator {
    //properties to save
    private String state;
    public String getState() { return state; }
    public void setState(String state) { this.state = state; }

    //create memo
    public Memento createMemento() { return new Memento(state); }
    //Restore the memo, import Memento and restore related data
    public void restoreMemento(Memento memento) { this.state = memento.getState(); }

    //Display Data
    public void print() { System.out.println("state = " + getState()); }
}
/
//memorandum
class Memento {
    //Memo data (memo data can be complex data)
    private String state;
    //Import data on creation
    public Memento(String state) { this.state = state; }
    //retrieve data
    public String getState() { return state; }
}

/
//manager
class Caretaker {
    //Memo object (more memo data can be saved through some data structures)
    private Memento memento;
    //get memo
    public Memento getMemento() { return memento; }
    //set memo
    public void setMemento(Memento memento) { this.memento = memento; }
}

Why use the memento pattern?

Capture the internal state of an object and save that state outside the object without breaking encapsulation.

advantage:
  • Encapsulation of information is achieved, so that users do not need to care about the details of state preservation.
  • Provide users with a mechanism that can restore the state, allowing users to return to a certain historical state more conveniently.
shortcoming:
  • If there are too many member variables or snapshots, relatively large resources will be occupied.

How to use memo mode?

scenes to be used

  • Undo operation: save/restore data related business scenarios;
    For example: when writing a document in Word, if you want to undo the previous input/delete operation, use Ctrl + Z to perform the undo operation;
  • State recovery: when "regret", restore the object to the previous state;
    Such as: archive use in the game;

Use in JDK

java.util.Date

When using the constructor Date(long date), this part of the data is backed up by passing in a piece of data.

The class itself that calls this constructor not only plays the role of initiator, but also plays the role of manager.

And this class corresponds to the role of memo.

public class Date
    implements java.io.Serializable, Cloneable, Comparable<Date>
{
    // used to store state
    private transient long fastTime;
    //constructor, save time
    public Date(long date) {
        fastTime = date;
    }
}

Tags: Java Design Pattern UML

Posted by markster on Sat, 03 Dec 2022 01:57:46 +0530