备忘录模式:
在不破换封装性的前提下,捕获一个对象的内部状态并将这个状态保存到对象外部,这样这个对象之后可以恢复到保存的状态。
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later
UML图:
主要包括:
这个设计模式很简单,就是面向对象封装这个特性的具体使用。
C++代码:
#include <iostream>
#include <string>
using namespace std;
class Memento
{
public:
Memento(string s)
{
state=s;
}
string getState()
{
return state;
}
private:
string state;
};
class Originator
{
public:
void setMemento(Memento *m)
{
state=m->getState();
}
Memento * createMemento(){
return new Memento(state);
}
void setState(string s)
{
state=s;
}
string getState()
{
return state;
}
private:
string state;
};
class Caretaker
{
public:
Memento* getMemento()
{
return memento;
}
void setMemento(Memento *m)
{
memento=m;
}
private:
Memento * memento;
};
int main()
{
Originator * originator=new Originator();
originator->setState("origin state");
std::cout<<"state 1:"<<originator->getState()<<std::endl;
Caretaker * caretaker=new Caretaker();
caretaker->setMemento(originator->createMemento());
originator->setState("change to state");
std::cout<<"state 2:"<<originator->getState()<<std::endl;
originator->setMemento(caretaker->getMemento());
std::cout<<"state 3:"<<originator->getState()<<std::endl;
return 0;
}
执行输出:
下面是一个以具体的例子。
UML类图:
C++代码如下:
#include <string>
#include <iostream>
using namespace std;
class Memento
{
public:
Memento(string n,string p,double b)
{
name=n;
phone=p;
budget=b;
}
string getName()
{
return name;
}
string getPhone()
{
return phone;
}
double getBudget()
{
return budget;
}
private:
string name;
string phone;
double budget;
};
class SalesProspect
{
public:
void setName(string n)
{
name=n;
}
void setPhone(string p)
{
phone=p;
}
void setBudget(double b)
{
budget=b;
}
string getName()
{
return name;
}
string getPhone()
{
return phone;
}
double getBudget()
{
return budget;
}
Memento* saveMemento()
{
return new Memento(name,phone,budget);
}
void restoreMemento(Memento * m)
{
name=m->getName();
phone=m->getPhone();
budget=m->getBudget();
}
private:
string name;
string phone;
double budget;
};
class ProspectMemory
{
public:
Memento * getMemento()
{
return memento;
}
void setMemento(Memento * m)
{
memento=m;
}
private:
Memento * memento;
};
int main()
{
SalesProspect * sale=new SalesProspect();
sale->setName("John");
sale->setPhone("15245869785");
sale->setBudget(1200);
std::cout<<"base sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;
ProspectMemory * prospect=new ProspectMemory();
prospect->setMemento(sale->saveMemento());
sale->setName("Mike");
sale->setPhone("13685478523");
sale->setBudget(1500);
std::cout<<"change sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;
sale->restoreMemento(prospect->getMemento());
std::cout<<"restore sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;
delete sale;
delete prospect;
return 0;
}
执行输出:
原文地址:http://blog.csdn.net/u012501459/article/details/46428389