//
// Created by win10 on 2021/11/16.
//
#include <thread>
#include <iostream>
#include <vector>
using namespace std;
/*
* 备忘录模式
* 在不破坏封装性的前提下, 捕获一个对象的内部状态, 并在该对象之外保存这个状态, 这样就可以将以后的对象状态恢复到先前保存的状态
*/
class Memo;
class Originator {
public:
Memo* createMemo();
void setMemo(Memo* memo);
void show() {
std::cout << "state: " << state << std::endl;
}
string state;
};
// 备忘录类
class Memo {
public:
Memo(string state) {
this->state = state;
}
string state;
};
Memo* Originator::createMemo() {
return new Memo(this->state);
}
void Originator::setMemo(Memo* memo) {
this->state = memo->state;
}
class Caretaker {
public:
Memo* memo;
};
int main() {
Originator* originator=new Originator();
originator->state = "originator";
originator->show();
Caretaker* caretaker = new Caretaker();
caretaker->memo = originator->createMemo();
originator->state = "off";
originator->show();
originator->setMemo(caretaker->memo);
originator->show();
return 0;
}
|