[Design Patterns] Chapter 21 Memo Pattern

1. Overview of the memorandum model

Definition:
Memo mode : capture the internal state of an object without breaking the encapsulation, and save this state outside the object, so that the object can be restored to the original saved state later.

The memo mode provides a mechanism for state recovery, so that users can easily return to a specific historical step. When the new state is invalid or has problems, the state can be restored using the temporarily saved memo. Many current software The memento pattern is used in the provided undo operation.

Second, the structure and implementation of the memorandum model

2.1 Structure of Memento Mode

The memento pattern consists of the following 3 roles:

  1. Originator : a common class that stores the current internal state by creating a memo, and can also use the memo to restore its internal state;
  2. Memento : used to store the internal state of the originator;
  3. Caretaker (person in charge ): Responsible for keeping the memo, but cannot operate or check the content of the memo.

2.2 Implementation of memo mode

// originator

/**
 * 象棋类,充当原发器
 */
public class Chessman {

    private String label;
    private int x;
    private int y;

    public Chessman(String label,int x,int y){
        this.label =label;
        this.x = x;
        this.y = y;
    }

    //保存备忘
    public ChessmanMemento save(){
        return new ChessmanMemento(this);
    }

    //撤销操作
    public void restore(ChessmanMemento memento){
        this.label = memento.getLabel();
        this.x = memento.getX();
        this.y = memento.getY();
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

//memorandum

/**
 * 象棋备忘录,充当备忘录角色
 */
public class ChessmanMemento {
    private String label;
    private int x;
    private int y;

    public ChessmanMemento(Chessman chessman){
        this.label = chessman.getLabel();
        this.x = chessman.getX();
        this.y = chessman.getY();
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

//principal

/**
 * 负责人角色
 */
public class MementoCaretaker {

    private ChessmanMemento chessmanMemento;

    public ChessmanMemento getChessmanMemento() {
        return chessmanMemento;
    }

    public void setChessmanMemento(ChessmanMemento chessmanMemento) {
        this.chessmanMemento = chessmanMemento;
    }
}

//client

public class Client {

    public static void main(String[] args) {

        /**
         * 案例需求描述:
         * 在一款运行在Android平台的触碰式中国象棋软件,有些用户经常走错棋或误操作,
         * 因此提供一个"悔棋"功能,用户走错棋之后可以恢复到前一个步骤。
         * 请使用备忘录模式实现此功能。
         *
         */

        MementoCaretaker mc = new MementoCaretaker();

        Chessman chessman = new Chessman("车", 1, 1);
        display(chessman);
        mc.setChessmanMemento(chessman.save());//保存状态,下棋之前
        chessman.setX(3);
        display(chessman);
        mc.setChessmanMemento(chessman.save());//保存状态,下棋之前
        chessman.setY(5);
        display(chessman);
        System.out.println("*****悔棋*****");
        chessman.restore(mc.getChessmanMemento());
        display(chessman);

    }

    public static void display(Chessman chessman) {
        System.out.println("棋子:" + chessman.getLabel() + "当前为止为:第" + chessman.getX() + "行,第" + chessman.getY() + "列");
    }

}

3. The advantages and disadvantages of the memorandum model and the applicable environment

3.1 Advantages of memo mode

  1. Provides an implementation mechanism for state recovery, allowing users to easily return to a specific historical step;
  2. It also realizes the encapsulation of information

3.2 Disadvantages of memo mode

  1. If there are too many member variables of the originator class that need to be saved, it will inevitably occupy a large amount of storage resources.

3.3 Applicable environment of memo mode

  1. Save all or part of the state of an object at a certain moment, so that it can be restored to the previous state when needed later, and undo operations can be realized;
  2. Prevent external objects from destroying the encapsulation of an object's historical state, and avoid exposing the implementation details of the object's historical state to external objects.

[References]:
This article is based on the study notes of Liu Wei's "Java Design Patterns". It is for learning purposes only. Do not use it for other purposes. Please respect intellectual property rights.

[Code warehouse for this article]: https://gitee.com/xiongbomy/java-design-pattern.git

Guess you like

Origin blog.csdn.net/weixin_44143114/article/details/126533112