Design Patterns - Memo Pattern Usage Example in Java - Chess Regret

Scenes

memo mode

The memo mode provides a mechanism for state recovery, allowing users to easily return to a specific historical step, when the new state is invalid

Or when there is a problem, you can use the temporarily stored memo to restore the status. Currently, many software provide Undo operation.

Among them, the memo mode is used.

Memo pattern structure diagram

The following roles are included in the memorandum pattern structure diagram:

Originator:

It is a common class that can create a memo and store its current internal state, or use the memo to restore its internal state,

Generally, classes that need to save internal state are designed as originators.

Memento:

Store the internal state of the originator, and decide which internal state to save according to the originator. The design of the memorandum can generally refer to the design of the original generator,

Determine the attributes in the memo class according to actual needs. It should be noted that, apart from the originator itself and the responsible human beings,

The memento object cannot be directly used by other classes, and the design of the originator will be implemented differently in different programming languages.

Caretaker (person in charge):

The person in charge, also known as the manager, is responsible for saving the memorandum, but cannot operate or check the contents of the memorandum.

One or more memo objects can be stored in the responsible person, it is only responsible for storing objects, but cannot modify objects,

There is also no need to know the implementation details of the object.

Note:

Blog:
Domineering Rogue Temperament_C#, Architecture Road, SpringBoot-CSDN Blog

accomplish

Use the memo mode to realize the chess undo function.

1. Create a new chess flag class as the originator

import lombok.Data;

//象棋旗子类:原发器
@Data
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.label,this.x,this.y);
    }

    //恢复状态
    public void restore(ChessmanMemento memento){
        this.label = memento.getLabel();
        this.x = memento.getX();
        this.y = memento.getY();
    }
}

2. Create a new chess piece memo class

import lombok.Data;

//象棋棋子备忘录类:备忘录
@Data
public class ChessmanMemento {

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

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

3. Create a new chess piece memorandum management class

//象棋棋子备忘录管理类:负责人
public class MementoCaretaker {

    private ChessmanMemento memento;

    public ChessmanMemento getMemento(){
        return memento;
    }

    public void setMemento(ChessmanMemento memento){
        this.memento = memento;
    }
}

4. Client calling method

public class Client {
    public static void main(String[] args) {
        MementoCaretaker mc = new MementoCaretaker();
        Chessman chess = new Chessman("车",1,1);
        display(chess);
        //保存状态
        mc.setMemento(chess.save());
        chess.setY(4);
        display(chess);
        //保存状态
        mc.setMemento(chess.save());
        chess.setX(5);
        display(chess);
        System.out.println("悔棋");
        //恢复状态
        chess.restore(mc.getMemento());
        display(chess);
    }

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

5. Summary

The main advantages of the memento pattern are as follows:

(1) It provides an implementation mechanism for state recovery, so that users can easily return to a specific historical step, when the new state is invalid

Or when there is a problem, you can use the temporarily stored memo to restore the state.

(2) The memo implements the encapsulation of information. A memo object is a representation of the state of the originator object and will not be changed by other codes.

The memo saves the state of the originator, and using a collection such as a list or a stack to store the memo object can realize multiple undo operations.

The main disadvantages of the memento pattern are as follows:

The resource consumption is too large. If there are too many member variables of the originator class that need to be saved, it will inevitably take up a large amount of storage space.

Saving the state of an object requires a certain amount of system resources.

Consider using the memento pattern in the following situations:

(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 the undo operation 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.

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/131991952