I want to learn the memo pattern of design patterns


This is the 8th day of my participation in the August Text Challenge.More challenges in August


Memo mode: A behavior mode that stores a state and retrieves it as needed, rather than double-counting.


What is the memo mode?

Memo mode: A behavior mode that stores a state and retrieves it as needed, rather than double-counting.

Note: Memorandum mode implementation cannot break the original encapsulation. In other words, you can take the internal state and keep it externally.


The advantages and disadvantages

advantages
  • It provides an implementation mechanism for state recovery that allows users to easily go back to a specific historical step, and when a new state is invalid or problematic, the state can be restored using a temporarily stored memo.
  • When the state of the initiator role changes, it is possible that the state is invalid, and the state can be restored using a temporarily stored memo.
disadvantages
  • If the state of the sponsor role needs to be fully stored in the memo object, the memo object can be expensive in terms of resource consumption.
  • If too many member variables of the original developer class need to be saved, it will inevitably occupy a large amount of storage space. Every time the state of the object is saved, certain system resources will be consumed.

Application scenarios

The most typical example is the Fibonacci sequence recursive implementation.

Without the help of the memo mode, the data is big, easy to burst the stack; With memos, the time complexity of the algorithm can be reduced to O(n). In addition, data caching is also a common application scenario.


implementation

Scene: chess example, can play chess, repent chess, undo repent chess and so on

@Data
@AllArgsConstructor
class Chessman {
    private String label;
    private int x;
    private int y;

    // Save the state
    public ChessmanMemento save(a) {
        return new ChessmanMemento(this.label, this.x, this.y);
    }

    // Restore the status
    public void restore(ChessmanMemento memento) {
        this.label = memento.getLabel();
        this.x = memento.getX();
        this.y = memento.getY();
    }

    public void show(a) {
        System.out.println(String.format("Pawn <%s> : current position is: <%d, %d>".this.getLabel(), this.getX(), this.getY())); }}class MementoCaretaker {
    // Define a collection to store memos
    private ArrayList mementolist = new ArrayList();

    public ChessmanMemento getMemento(int i) {
        return (ChessmanMemento) mementolist.get(i);
    }

    public void addMemento(ChessmanMemento memento) { mementolist.add(memento); }}@Data
@AllArgsConstructor
class ChessmanMemento {
    private String label;
    private int x;
    private int y;
}
class Client {
    private static int index = -1;
    private static MementoCaretaker mc = new MementoCaretaker();

    public static void main(String args[]) {
        Chessman chess = new Chessman("Car".1.1);
        play(chess);
        chess.setY(4);
        play(chess);
        chess.setX(5);
        play(chess);
        undo(chess, index);
        undo(chess, index);
        redo(chess, index);
        redo(chess, index);
    }

    // Play chess while saving the memo
    public static void play(Chessman chess) {
        mc.addMemento(chess.save());
        index++;
        chess.show();
    }

    // Undo the last memo
    public static void undo(Chessman chess, int i) {
        System.out.println(Back "* * * * * * * * * * * *");
        index--;
        chess.restore(mc.getMemento(i - 1));
        chess.show();
    }

    // Undo the contrition and proceed to the next memo
    public static void redo(Chessman chess, int i) {
        System.out.println("****** undo regret ******");
        index++;
        chess.restore(mc.getMemento(i + 1)); chess.show(); }}Copy the code