class Memento
{
public:
void setState(State* state) { m_state = state; }
State* state() const { return m_state; }
private:
friend class Viewer;
Memento() { }
private:
State* m_state;
};
class Viewer
{
public:
void drawGraphics();
Memento* createMemento();
void setMemento(Memento* memento);
};
Memento* Viewer::createMemento()
{
State* state = getSelfState();
Memento* memento = new Memento();
memento->setState(state);
return memento;
}
void setMemento(Memento* memento)
{
setSelfState(memento->state());
}
class Command
{
public:
void execute();
void unExecute();
private:
Viewer* m_viewer;
std::vector<Memento*> m_mementos;
};
void Command::execute()
{
m_viewer->drawGraphics();
m_mementos.push_back(m_viewer->createMemento());
}
void Command::unExecute()
{
if(!m_memento.isEmpty())
{
m_viewer->setMemento(m_mementos.back());
m_mementso.pop_back();
}
}
Memento - 备忘录模式,布布扣,bubuko.com
原文:http://blog.csdn.net/harrising/article/details/38052709