Management State of Design Patterns _Memento Memorandum Mode _Save Object State

Preface

Blogger github

Blogger's personal blog http://blog.healerjean.com

1. Interpretation

When using object-oriented programming, we need to save the relevant state information of strength first, and then revoke, we can restore to the original state according to the information retained.

Memo mode you have certainly seen, for example, when playing a game, there is a function to save the status of the current user, which will save the status of the current user. When the user fails to break through or needs to start from the snapshot, the saved state can be read and restored completely to the environment at that time. This is similar to the snapshot function on VMware

When we write a file with a text editor, if we delete a sentence carelessly, we can restore the file to its previous state through CTRL+Z, and some text editors can revoke it many times.

1.1. The Difference between Memorandum Mode and Replication Mode

** Memorandum mode is used to save and restore the state of objects, so what is the difference between them? * *

The prototype schema stores all the state information of the current object and generates another instance exactly the same as the saved object when restored; **

2. The memorandum mode preserves part of the state information of the object we are concerned about when recovering, which is equivalent to a snapshot.

2. Instance Code

2.1. Memento, Memorandum Content

/**
 * @author HealerJean
 * @version 1.0v
 * @ClassName Memento
 * @Date 2019/8/20  18:26.
 * @Description Memorandum role to store the costume of the initiator
 */
@Data
public class Memento {

    private String state;
    private float x;
    private float y;

    public Memento(String state, float x, float y) {
        this.state = state;
        this.x = x;
        this.y = y;
    }

}

2.2. Memorandum Manager, because there may be multiple undo operations

/**
 * @author HealerJean
 * @ClassName MementoManager
 * @date 2019/8/20  18:29.
 * @Description Memorandum Manager
 */
public class MementoManager {

    private List<Memento> mementos;



    public MementoManager(){
        mementos = new ArrayList<>();
    }

    public void add(Memento memento){
        mementos.add(memento);
    }


    public List<Memento> getMementoList() {
        return mementos;
    }

    /**
     * Through state recovery
     */
    public Memento getByState(String status) {
        for (Memento memento :mementos){
            if(status.equals(memento.getState())){
                return memento;
            }
        }
        return null ;
    }
}

2.3. Data to be saved and memorandums to be saved

@Data
public class Originator {

    private String state;
    private float x;
    private float y;

    /**
     * Keep it in a memorandum
     * @return
     */
    public Memento saveToMemento() {
        return new Memento(state, x, y);
    }

    /**
     * Recovery from the Memorandum
     * @param memento
     */
    public void restoreFromMemento(Memento memento) {
        this.state = memento.getState();
        this.x = memento.getX();
        this.y = memento.getY();
    }

}

2.4. Testing

public class D18Main {

    public static void main(String[] args) {

        //Data to be saved
        Originator originator = new Originator();

        //Memorandum management, responsible for storing historical status
        MementoManager manager = new MementoManager();
        originator.setState("2017-01-01");
        originator.setX(1.4f);
        originator.setY(5.4f);
        manager.add(originator.saveToMemento());

        originator.setState("2017-04-03");
        originator.setX(44.4f);
        originator.setY(52.4f);
        manager.add(originator.saveToMemento());

        originator.setState("2017-06-01");
        originator.setX(231.4f);
        originator.setY(555.4f);
        //Record status
        manager.add(originator.saveToMemento());

        originator.setState("2017-06-22");
        originator.setX(132.4f);
        originator.setY(53.4f);
        //Record status
        manager.add(originator.saveToMemento());

        System.out.println("Current status:");
        System.out.println(originator.getState() + ": " + originator.getX() + ", " + originator.getY());


        System.out.println("State History:");
        for (Memento m : manager.getMementoList()) {
            System.out.println(m.getState() + ": " + m.getX() + ", " + m.getY());
        }


        //Restore to the specified state -- 2017-04-03
        originator.restoreFromMemento(manager.getByState("2017-04-03"));
        System.out.println("The state after recovery:");
        System.out.println(originator.getState() + ": " + originator.getX() + ", " + originator.getY());
    }
}

3. Summary

Memorandum mode is usually used to store the state of a certain time, which is not usually stored in the database, but only at the code level. Through some processes, the state of the previous process can be found.



Interested, welcome to add blogger Wechat

Ha, the blogger is very happy to communicate with friends from all walks of life. If you are satisfied, please reward the amount of money the blogger intends to pay. Interested in Wechat transfer, please note your Wechat or other contact information. Add the blogger Weixin.

Please leave a message below. Discuss freely with bloggers

WeChat Wechat Public Number Alipay

Tags: snapshot github Programming Vmware

Posted on Mon, 26 Aug 2019 00:49:31 -0700 by takeiteasy