Memo mode (memo)


         This Friday, I attended the memo mode seminar. The purpose of this article is to summarize and share this mode, hoping to be helpful to students who need to learn this design mode. This paper is divided into two parts. The first part is the summary of the basic knowledge of the memo model; The second part is a simple example of the sharing memo mode.

1, Memo mode summary


1. Model intention

         Without breaking the encapsulation, capture the internal state of an object and save the state outside the object. This will restore the object to its original saved state later.

2. Participants

Memento memo
- the memo stores the internal state of the initiator object; Prevent objects other than the initiator from accessing the memo;
– memos actually have two interfaces. Managers can only see the narrow interface of memos, which can only pass memos to other objects; The initiator can see the wide interface of the memo, allowing it to access all the data required to return to the original state;
– the initiator creates a memo to record its internal state at the current time;
– use the memo to restore the internal state;
Caretaker Manager
– keep memos;
– the contents of the memorandum cannot be operated or checked;


3. Structure, cooperation, applicability and effect

Structure diagram:

1. When the manager needs the backup status, he requests the originator to create a memo and save it;
2. When the manager restores the state, set the state for the initiator. The initiator takes out the equipment from the memo and restores it to the saved state of the memo.

1. The (partial) state of an object at a certain time must be saved so that it can be restored to its previous state when necessary.
2. If an interface lets other objects get these states directly, it will expose the implementation details of the object and destroy the encapsulation of the object.
1. Maintain encapsulation boundaries.

Using memos can avoid exposing information that should only be managed by the originator but must be stored outside the originator. This pattern shields the possibly complex internal information of the initiator from other objects, so as to maintain the encapsulation boundary

2. Simplifies the primary generator.

The memo object is managed by the manager, which avoids that the originator contains the management source code of the memo, and simplifies the design of the originator.

3. Using memos can be costly.

If the originator must copy and store a large amount of information when generating memos, or the customer creates memos and restores the state of the originator very frequently, it may lead to very large overhead.

4. Define narrow and wide interfaces.

For some languages, it is difficult to guarantee that only the originator can access the state of the memo.

5. Potential costs of maintaining memos.

The manager is responsible for deleting the memos it maintains. However, the manager does not know how many states the memos have. Therefore, when storing memos, an otherwise small manager may incur a lot of storage overhead.

2, Simple example of memo mode

1. Use case description

         Generally, the game will provide the function of saving the game progress to save the current game character state. When it is necessary to roll back and play again, you only need to read the saved file to restore to the saved state. This is a typical use case scenario of memo mode. The following uses simple code to demonstrate backup and recovery (archiving / file reading) and understand how memo mode is used.     

2. Sample code

class Role;

//Role attribute memo
class RoleMemento {

    int get_level() {return m_level;}
    void set_level(int l) {m_level = l;}

    int get_attack() {return m_attack;}
    void set_attack(int a) {m_attack = a;}

    int get_defense() { return m_defense;}
    void set_defense(int d) {m_defense = d;}

    int get_vitality() {return m_vitality;}
    void set_vitality(int v) {m_vitality = v;}

    friend class Role;
    int m_level;   // Grade
    int m_attack;  // aggressivity
    int m_defense; // Defensive power
    int m_vitality;// Life value

//Game character
class Role {
    RoleMemento* save_role_state();
    void recover_role_state(RoleMemento* role_memento);
    void init();
    void fight(int n);

    int m_level;   // Grade
    int m_attack;  // aggressivity
    int m_defense; // Defensive power
    int m_vitality;// Life value

RoleMemento* Role::save_role_state() {
    RoleMemento* new_role = new RoleMemento();

    return new_role;

void Role::recover_role_state(RoleMemento* role_memento) {
    m_level = role_memento->get_level();
    m_attack = role_memento->get_attack();
    m_defense = role_memento->get_defense();
    m_vitality = role_memento->get_vitality();

//GameSave Manager 
class MementoManager {
    void set_memento(RoleMemento* memento) {m_role_memento = memento;}
    RoleMemento* get_memento() {return m_role_memento;}

    RoleMemento* m_role_memento;

MementoManager *memento_manager = new MementoManager;
Role* role_1 = new Role;
memento_manager->set_memento(role_1 ->save_role_state());
//play a game
//File reading


         Memo mode is usually used in conjunction with command mode. It saves the state before the command is executed and restores the state of the actuator when the command is revoked. This is also another classic use scenario of memo mode.

Tags: C++ Design Pattern

Posted on Sun, 05 Sep 2021 14:58:30 -0400 by davespring