Memo mode of design mode

Time:2022-5-29

Memo mode, also known as snapshot mode, is a behavioral mode. A snapshot that uses one memo object to store the internal state of another object. The memo mode is used to capture, externalize and store the state of an object without destroying the encapsulation, so that the object can be restored to the stored state at an appropriate time in the future.

Developers should be familiar with this model. The development software we use will have such memo functions. Pressing ctrl+z during editing can undo the current operation and restore the document to its previous state; The database also has the logging function of undo and redo. If an error is reported in a transaction, the data before modification can be returned.

The UML class diagram of memo pattern is as follows:

It can be seen from the above figure that the memo mode mainly involves three roles: memo role, originator role and caretaker role:

  • Originator role: create a memo object containing the current internal status, and use the memo object to store its internal status
  • Memo role: responsible for keeping records, that is, the internal status of the initiator role. The memo can determine how many internal states of the initiator object are stored according to the judgment of the initiator object
  • Caretaker role: responsible for saving memo objects without checking the contents of memo objects. General usage record saving memo object

Game character examples

The game characters have attack power and defense power. They save their own state (attack power and defense power) before the boss war. When the boss war is over, the attack power and defense power decrease, and they recover from the memo object to the state before the war.

Example UML class diagram:

Memo role:

package com.charon.Memento;

/**
 * @className: Memento
 *@description: memo role
 * @author: charon
 * @create: 2022-04-05 22:43
 */
public class Memento {

    private int vit,def;

    public Memento(int vit, int def) {
        this.vit = vit;
        this.def = def;
    }

    public Memento() {

    }

    /**
     * Gets the value of vit
     *
     * @return the value of vit
     */
    public int getVit() {
        return vit;
    }

    /**
     * Gets the value of def
     *
     * @return the value of def
     */
    public int getDef() {
        return def;
    }
}

Principal role:

package com.charon.Memento;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: Caretaker
 *@description: principal role
 * @author: charon
 * @create: 2022-04-05 22:50
 */
public class Caretaker {

    private Memento memento = new Memento();

    /**
     * Gets the value of mementos
     *
     * @return the value of mementos
     */
    public Memento getMemento() {
        return memento;
    }

    /**
     * Sets the mementos
     *
     * @param memento memento
     */
    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}

Initiator role:

package com.charon.Memento;

/**
 * @className: GameRole
 * @description:
 * @author: charon
 * @create: 2022-04-05 23:01
 */
public class GameRole {

    private int vit,def;

    public GameRole(int vit, int def) {
        this.vit = vit;
        this.def = def;
    }

    /**
     * Gets the value of vit
     *
     * @return the value of vit
     */
    public int getVit() {
        return vit;
    }

    /**
     * Sets the vit
     *
     * @param vit vit
     */
    public void setVit(int vit) {
        this.vit = vit;
    }

    /**
     * Gets the value of def
     *
     * @return the value of def
     */
    public int getDef() {
        return def;
    }

    /**
     * Sets the def
     *
     * @param def def
     */
    public void setDef(int def) {
        this.def = def;
    }

    /**
     *Create memo object
     * @return
     */
    public Memento createMemento(){
        return new Memento(vit,def);
    }

    /**
     *Restore the state of GameRole from the memo object
     * @param memento
     */
    public void recoverGameRoleFromMemento(Memento memento){
        this.vit = memento.getVit();
        this.def = memento.getDef();
    }

    public void display(){
        System Out Println ("the attack power of the current game character is:" + this.vit + "; the defense power is:" + this.def ");
    }

}

Test:

package com.charon.Memento;

/**
 * @className: Client
 * @description:  
 * @author: charon
 * @create: 2022-04-04 23:00
 */
public class Client {

    public static void main(String[] args) {
        //Create a game character
        GameRole role = new GameRole(100,100);
        System Out Println ("pre war state:");
        role.display();

        //Save current status
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(role.createMemento());

        System Out Println ("start typing boss:");
        role.setDef(70);
        role.setVit(70);
        role.display();

        System Out Println ("after the war, use the memo object to restore to the pre war");
        role.recoverGameRoleFromMemento(caretaker.getMemento());

        System Out Println ("status after recovery:");
        role.display();
    }
    
}

Print:
    Pre war status:
    The attack power of the current game character is: 100; Defence: 100
    Start to call boss:
    The attack power of the current game character is: 70; Defence Force: 70
    After the war, use the memo object to restore to the pre war
    Status after recovery:
    The attack power of the current game character is: 100; Defence: 100

The main advantages of memo mode are as follows:

  • Provides a mechanism to restore state. When users need it, they can easily restore the data to a historical state.
  • Realize the encapsulation of internal state. Except for the initiator who created it, no other object can access the status information.
  • Simplified the initiation of human. The initiator does not need to manage and save each backup of its internal status. All status information is saved in the memo and managed by the manager, which conforms to the principle of single responsibility.

Its main disadvantages are:

  • Large resource consumption. If the internal state information to be saved is too much or too frequent, it will occupy a large amount of memory resources.

Application scenarios of memo mode

  1. Scenes that need to save and restore data, such as the archiving function of intermediate results when playing games.
  2. You need to provide a scenario for rollback operations, such as pressing ctrl+z when editing word, Notepad, Photoshop, eclipse and other software, as well as transaction operations in the database.

Relationship between memo mode and command mode

If the state storage of an object’s revocable operation is involved, the memo mode is not enough, and the command mode should be considered.

If the command mode is used in a system and the command revocation function needs to be implemented, the command mode can use the memo mode to store the status of the revocable operation.

Recommended Today

RabbitMQ study notes

Table of contents Related concepts RabbitMQ Install RabbitMQ core part Hello World Work Queues release confirmation switch fanout exchange (pub/sub) direct exchange topic exchange dead letter queue delay queue Release Confirmation Advanced The switch receives the message confirmation callback Fallback message to producer when switch is not routable Backup switch other idempotency priority queue lazy […]