【1】什么是备忘录模式? 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。 这样就可以将该对象恢复到原先保存的状态。 【2】备忘录模式的代码示例: 代码示例如下: #include <iostream> #include <string> #include <vector> using namespace std; class Memo { public: string state; Memo(string state) { this->state = state; } }; class Originator { public: string state; void setMemo(Memo *memo) { state = memo->state; } Memo *createMemo() { return new Memo(state); } void show() { cout << state << endl; } }; class Caretaker { public: vector<Memo *> memo; public: void save(Memo *memo) { (this->memo).push_back(memo); } Memo *getState(int i) { return memo[i]; } }; int main() { Originator *og = new Originator(); Caretaker *ct = new Caretaker(); og->state = "on"; og->show(); ct->save(og->createMemo()); og->state = "off"; og->show(); ct->save(og->createMemo()); og->state = "middle"; og->show(); ct->save(og->createMemo()); og->setMemo( ct->getState(1) ); og->show(); return 0; } //Result: /* on off middle off */
http://www.cnblogs.com/Braveliu/p/3946865.html