备忘录模式(Memento)

备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.

备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态.

#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 << "消灭1号怪物" << endl;
    }

    void killBoss1()
    {
        if(mCurrentPlayer.mHp<30)
        {
            cout << "实力不足,被boss1消灭" << endl;
            mCurrentPlayer.mHp = 10;
            return;
        }
        cout << "消灭boss1" << endl;
    }

    void killBoss2()
    {
        if(mCurrentPlayer.mHp<300)
        {
            cout << "实力不足,被boss2消灭" << endl;
            mCurrentPlayer.mHp = 10;
            return;
        }
        cout << "消灭boss2" << endl;
    }

    //创建存档
    void createMemento(int num)
    {
        mNum = num;
        mPlayerMap.insert(make_pair<int,Player>(mNum,mCurrentPlayer));
        cout << "将当前环境存档,编号:" << num << endl;
    }

    //档读存储
    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 << "导入一个强力的存档" << endl;
    }

    //读取存档
    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 << "读取存档:" << num << endl;
            }
        }
    }

private:
    Player mCurrentPlayer;

    map<int,Player> mPlayerMap;

    int mNum;
};

int main()
{
    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();
}

消灭1号怪物
将当前环境存档,编号:1
实力不足,被boss1消灭
读取存档:1
当前角色,hp:20,mp:20,ap:20
消灭1号怪物
消灭boss1
实力不足,被boss2消灭
导入一个强力的存档
读取存档:1000
消灭boss2

猜你喜欢

转载自xiangjie88.iteye.com/blog/2096614