Memento

Memento: Capture the internal state of an object and save this state outside the object without destroying encapsulation. This way, the object can be restored to the original saved state later. The

memento pattern is more suitable for The function is more complex, but the class that needs to maintain or record the attribute history, or when the attributes to be saved are only a small part of the many attributes, the Originator can restore to the previous state according to the saved Memento information.

#include <iostream>
#include <map>
using namespace std;

class Player
{
public:
    int mHp;
    int mMp;
    int mAp;
};

class Game
{
public:
    Game()
    {
        mCurrentPlayer.mAp = 10;
        mCurrentPlayer.mHp = 10;
        mCurrentPlayer.mMp = 10;
        mNum = 1;
    }

    ~Game(){}

    void showStatus()
    {
        cout << "当前角色,hp:" << mCurrentPlayer.mHp << ",mp:" << mCurrentPlayer.mMp
             << ",ap:" << mCurrentPlayer.mAp << endl;
    }

    void killNPC1()
    {
        mCurrentPlayer.mAp += 10;
        mCurrentPlayer.mHp += 10;
        mCurrentPlayer.mMp += 10;
        cout << "Destroy monster 1" << endl;
    }

    void killBoss1()
    {
        if(mCurrentPlayer.mHp<30)
        {
            cout << "Insufficient strength, destroyed by boss1" << endl;
            mCurrentPlayer.mHp = 10;
            return;
        }
        cout << "消灭boss1" << endl;
    }

    void killBoss2()
    {
        if(mCurrentPlayer.mHp<300)
        {
            cout << "Insufficient strength, destroyed by boss2" << endl;
            mCurrentPlayer.mHp = 10;
            return;
        }
        cout << "消灭boss2" << endl;
    }

    //create archive
    void createMemento(int num)
    {
        mNum = num
        mPlayerMap.insert(make_pair<int,Player>(mNum,mCurrentPlayer));
        cout << "Save the current environment, number:" << num << endl;
    }

    //File read storage
    void loadMemento(const map<int,Player>& playerMap)
    {
        mPlayerMap.clear();
        for(map<int,Player>::const_iterator iter = playerMap.begin();
                                            iter!=playerMap.end();++iter)
        {
            mPlayerMap.insert(make_pair<int,Player>((*iter).first,(*iter).second));
        }
        cout << "Import a powerful archive" << endl;
    }

    // read archive
    void restoreMemento(int num)
    {
        for(map<int,Player>::const_iterator iter = mPlayerMap.begin();
                                            iter!=mPlayerMap.end();++iter)
        {
            if((*iter).first==num)
            {
                mCurrentPlayer.mAp = (*iter).second.mAp;
                mCurrentPlayer.mHp = (*iter).second.mHp;
                mCurrentPlayer.mMp = (*iter).second.mMp;
                cout << "Read archive:" << num << endl;
            }
        }
    }

private:
    Player mCurrentPlayer;

    map<int,Player> mPlayerMap;

    int mNum;
};

intmain()
{
    Game* game = new Game;
    game->killNPC1();
    game->createMemento(1);
    game->killBoss1();
    game->restoreMemento(1);
    game->showStatus();
    game->killNPC1();
    game->restoreMemento(2);
    game->killBoss1();
    game->killBoss2();
    map<int,Player> m;
    Player seniorPlayer;
    seniorPlayer.mAp = 1000;
    seniorPlayer.mHp = 1000;
    seniorPlayer.mMp = 1000;
    m.insert(make_pair<int,Player>(1000,seniorPlayer));
    game->loadMemento(m);
    game->restoreMemento(1000);
    game->killBoss2();
}

Destroy monster number 1
Archive the current environment, ID: 1
Insufficient strength, destroyed by boss1
Read Archives: 1
Current character, hp: 20, mp: 20, ap: 20
Destroy monster number 1
kill boss1
Insufficient strength, destroyed by boss2
Import a strong archive
Read Archives: 1000
kill boss2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326509233&siteId=291194637