备忘录模式.cpp
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
namespace ns1
{
class FighterMemento
{
friend class Fighter;
int m_life;
int m_magic;
int m_attack;
private:
FighterMemento(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
private:
int getLife() const { return m_life; }
void setLife(int life) { m_life = life; }
int getMagic() const { return m_magic; }
void setMagic(int magic) { m_magic = magic; }
int getAttack() const { return m_attack; }
void setAttack(int attack) { m_attack = attack; }
};
class Fighter
{
int m_life;
int m_magic;
int m_attack;
public:
Fighter(int life, int magic, int attack) : m_life(life), m_magic(magic), m_attack(attack) {}
public:
shared_ptr<FighterMemento> createMomento() const
{
shared_ptr<FighterMemento> back(new FighterMemento(m_life, m_magic, m_attack));
return back;
}
void restoreMomento(const shared_ptr<FighterMemento> &pfm)
{
m_life = pfm->m_life;
m_magic = pfm->getMagic();
m_attack = pfm->getAttack();
}
void setToDead() { m_life = 0; }
void displayInfo() const
{
cout << "The player's current HP, magic and attack power are respectively " << m_life << ", " << m_magic << ", " << m_attack << endl;
}
};
class FCareTaker
{
shared_ptr<FighterMemento> m_pfm;
public:
FCareTaker(const shared_ptr<FighterMemento> &ptmpfm = nullptr) : m_pfm(ptmpfm) {}
shared_ptr<FighterMemento> getMemento() const { return m_pfm; }
void setMemento(const shared_ptr<FighterMemento> &ptmpfm) { m_pfm = ptmpfm; }
};
class FCareTaker2
{
vector<shared_ptr<FighterMemento>> m_pfmContainer;
public:
void push(const shared_ptr<FighterMemento> &ptmpfm) { m_pfmContainer.emplace_back(ptmpfm); }
shared_ptr<FighterMemento> pop()
{
if (m_pfmContainer.empty())
return nullptr;
return m_pfmContainer.back();
}
shared_ptr<FighterMemento> getMemento(int index) const
{
if (index >= 0 && index < m_pfmContainer.size())
return m_pfmContainer[index];
return nullptr;
}
};
}
int main()
{
#if 0
using namespace ns1;
shared_ptr<Fighter> p_fighter(new Fighter(800, 200, 300));
p_fighter->displayInfo();
shared_ptr<FighterMemento> p_fighterMemo = p_fighter->createMomento();
cout << "The protagonist of the player and BOSS began to fight fiercely------" << endl;
p_fighter->setToDead();
p_fighter->displayInfo();
cout << "Players recover their information through memos------" << endl;
p_fighter->restoreMomento(p_fighterMemo);
p_fighter->displayInfo();
#endif
#if 0
using namespace ns1;
shared_ptr<Fighter> p_fighter(new Fighter(800, 200, 300));
p_fighter->displayInfo();
shared_ptr<FCareTaker> pfcaretaker(new FCareTaker(p_fighter->createMomento()));
cout << "The protagonist of the player and BOSS began to fight fiercely------" << endl;
p_fighter->setToDead();
p_fighter->displayInfo();
cout << "Players recover their information through memos------" << endl;
p_fighter->restoreMomento(pfcaretaker->getMemento());
p_fighter->displayInfo();
#endif
#if 1
using namespace ns1;
shared_ptr<Fighter> p_fighter2(new Fighter(800, 200, 300));
shared_ptr<FCareTaker2> pfcaretaker2(new FCareTaker2());
pfcaretaker2->push(p_fighter2->createMomento());
p_fighter2->setToDead();
pfcaretaker2->push(p_fighter2->createMomento());
p_fighter2->displayInfo();
cout << "------------------" << endl;
p_fighter2->restoreMomento(pfcaretaker2->getMemento(0));
p_fighter2->displayInfo();
#endif
cout << "Over!\n";
return 0;
}