(3) Behavior pattern: 6. Memento Pattern (C++ example)

Table of contents

1. The meaning of Memento Pattern

2. UML diagram learning in memo mode

3. Application scenarios of memo mode

4. Advantages and disadvantages of memo mode

(1) Advantages:

(2) Disadvantages

5. Examples of implementing memo pattern in C++


1. The meaning of Memento Pattern

Memento: Capture the internal state of an object and save this state outside the object without destroying encapsulation. This allows you to later restore the object to its original saved state. 【DP】

Memento Pattern is a behavioral design pattern that is used to capture and store the internal state of an object without destroying encapsulation, and restore that state when needed. It saves the object's state to the memo object, which can then be restored through the memo object.

2. UML diagram learning in memo mode

 The main roles of the memo pattern include:

(1) Originator : Responsible for creating a memo object to record the internal state of the current object, and can use the memo object to restore its state.

(2) Memento : used to store the internal state of the Originator object.

(3) Caretaker (Manager) : Responsible for saving the memo object, but cannot modify or check the memo object.

3. Application scenarios of memo mode

(1) The historical state of the object needs to be saved and restored.

(2) A system that needs to provide undo or rollback operations.

(3) Systems that need to take snapshots of the status of objects and compare them.

4. Advantages and disadvantages of memo mode

(1) Advantages:

        1) Provides a reliable way to save and restore the state of an object, avoiding directly exposing the internal details of the object.

        2) The memo object is decoupled from the original object, so that the original object can focus on business logic without having to care about saving and restoring state.

        3) The functions of the memo object can be flexibly extended, such as supporting the saving and restoration of multiple historical states.

(2) Disadvantages

        1) If the status of the original object is large or changes frequently, it will occupy a large memory space.

        2) When saving and restoring state, object serialization and deserialization operations are required, which may affect performance.

5. Examples of implementing memo pattern in C++


#include <iostream>
#include <string>

// 备忘录类
class Memento {
public:
    Memento(const std::string& state) : state_(state) {}

    std::string getState() const {
        return state_;
    }

private:
    std::string state_;
};

// 发起人类
class Originator {
public:
    void setState(const std::string& state) {
        state_ = state;
    }

    std::string getState() const {
        return state_;
    }

    Memento createMemento() {
        return Memento(state_);
    }

    void restoreMemento(const Memento& memento) {
        state_ = memento.getState();
    }

private:
    std::string state_;
};

// 管理者类
class Caretaker {
public:
    void saveMemento(const Memento& memento) {
        memento_ = memento;
    }

    Memento retrieveMemento() const {
        return memento_;
    }

private:
    Memento memento_;
};

int main() 
{
    Originator originator;
    Caretaker caretaker;

    // 设置初始状态
    originator.setState("State 1");
    std::cout << "Current state: " << originator.getState() << std::endl;

    // 保存状态
    caretaker.saveMemento(originator.createMemento());

    // 修改状态
    originator.setState("State 2");
    std::cout << "Current state: " << originator.getState() << std::endl;

    // 恢复状态
    originator.restoreMemento(caretaker.retrieveMemento());
    std::cout << "Restored state: " << originator.getState() << std::endl;

    return 0;
}

In the above example, the Originator class represents the initiator, the Caretaker class represents the manager, and the Memento class represents the memo. You can create a memo object by calling the createMemento method, and restore the state of the object through the restoreMemento method.

Guess you like

Origin blog.csdn.net/bigger_belief/article/details/132414186