#pragma once
#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
/*
设计模式-备忘录模式(Memento)
在不破坏对象封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
这样以后就可以将该对象恢复到原先保存的状态。
*/
class Cmemento {//备忘录
public:
string mstrState="";
public:
Cmemento(const string &strState) {
mstrState = strState;
}
};
class COriginator {//发起人
public:
string mstrState="";
public:
Cmemento * CreateMemento() {
return new Cmemento(mstrState);
}
void SetMemento(Cmemento * pMemento) {
mstrState = pMemento->mstrState;
}
void Show() {
cout << "State=" << mstrState << endl;
}
};
class CCaretaker {
public:
Cmemento *mpMemento=NULL;
};
int main() {
COriginator *P0 = new COriginator();
P0->mstrState = "On";
P0->Show();
CCaretaker *pC = new CCaretaker();
pC->mpMemento = P0->CreateMemento();
P0->mstrState = "Off";
P0->Show();
P0->SetMemento(pC->mpMemento);
P0->Show();
getchar();
return 0;
}