Memento 模式
Memento 模式的关键就是要在不破坏封装行的前提下,捕获并保存一个类的内部状态,这样就可以利用该保存的状态实施恢复操作。

/////////Originator.h//////////////////////////////////////////
#pragma once
#include <string>
using namespace std;
typedef string State ; class Memento ;
class Originator
{
public:
Originator();
Originator(const State& std);
~Originator();
Memento* GreatMemento();
void SetMemento();
void RestoreToMemento();
State GetState();
void SetState(const State& sdt);
void PrintState(); protected:
private:
State _sdt ;
Memento* _mt;
};
/////////Memento.h//////////////////////////////////
#pragma once
#include <string>
using namespace std; typedef string State ; class Memento
{
public:
protected:
private:
friend class Originator ;
Memento();
Memento(const State& sdt);
~Memento();
void SetState(const State& sdt);
State GetState();
State _sdt ; };
////////////Originator.cpp/////////////////////////////////////////////////
#include "Originator.h"
#include "Memento.h"
#include <iostream>
using namespace std;
Originator::Originator(const State& std)
{
_sdt = std ;
_mt = ;
}
Originator::Originator()
{
_sdt = "" ;
_mt = ;
}
void Originator::SetMemento()
{
if (_mt == )
{
this->_mt = GreatMemento();
}
else
{
_mt->SetState(_sdt);
} }
Memento* Originator::GreatMemento()
{
return new Memento(_sdt);
}
void Originator::RestoreToMemento()
{
if (this->_mt != )
{
_sdt = _mt->GetState();
} }
void Originator::SetState(const State& sdt)
{
_sdt = sdt ;
} State Originator::GetState()
{
return _sdt;
} void Originator::PrintState()
{
cout<<this->_sdt<<"....."<<endl;
}
Originator::~Originator()
{
delete _mt;
}
///////////Memento.cpp/////////////////////////
#include "Memento.h"
#include <string>
using namespace std; typedef string State ; Memento::Memento(const State& sdt)
{
_sdt = sdt ;
}
Memento::Memento()
{ }
void Memento::SetState(const State& sdt)
{
_sdt = sdt ;
} State Memento::GetState()
{
return _sdt ;
}
Memento::~Memento()
{ }
///////////////main.cpp///////////////////////////////////////////////////////////
#include "Memento.h"
#include "Originator.h"
#include <string>
#include <iostream>
using namespace std; typedef string State ; int main()
{
Originator* O = new Originator();
O->SetState("备忘前状态 Old");
O->PrintState(); O->SetMemento();//备忘 O->SetState("备忘后状态 New");
O->PrintState(); O->RestoreToMemento();//恢复 O->PrintState(); getchar();
return ;
}
Memento 模式的更多相关文章
- 【行为型】Memento模式
备忘录模式顾名思义就是一种能有备忘作用的设计模式,其目的是在对象外部保存其在某一时刻的状态信息,并且在任何需要的时候,都可以通过备忘录中保存的状态数据恢复对象在当时情形下的状态. 备忘录模式旨在对象的 ...
- Java设计模式(15)备忘录模式(Memento模式)
Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public class ...
- 设计模式之——Memento模式
Memento模式即快照模式,就是在某一时刻,设定一个状态,在后面随时可以返回到当前状态的模式. 我们拿一个闯关游戏作为举例,一共有十关,每闯一关,玩家所持金额增加一百,而闯关失败就扣一百.初始时,给 ...
- memento模式
参考资料 • 维基百科:https://en.wikipedia.org/wiki/Memento_pattern • 百度百科:http://baike.baidu.com/link?url=ZQZ ...
- Memento模式(备忘录设计模式)
Memento模式? 使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态.这个时候你需要使用Memento设计模式.(以 ...
- C++设计模式实现--备忘录(Memento)模式
一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态. 这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比較适用于功 ...
- Behavioral模式之Memento模式
1.意图 在不破坏封装性的前提下,捕获一个对象的内部状态.并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. 2.别名 Token 3.动机 有时候有必要记录一个对象的内部状态.为 ...
- 设计模式(十八)Memento模式
在使用面向对象编程的方式实现撤销功能时,需要事先保存实例的相关状态信息.然后,在撤销时,还需要根据所保存的信息将实例恢复至原来的状态. 要想恢复实例,需要一个可以自由访问实例内部结构的权限.但是,如果 ...
- 设计模式C++描述----17.备忘录(Memento)模式
一. 备忘录模式 定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 结构图: 使用范围: Memento 模式比较适用于功能 ...
随机推荐
- jQuery 参考手册 - 事件
事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. bind()向匹配元素附加一个或更多事件处理器 $(selector).bind(event,function) $(select ...
- HW4.28
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Yii PHP 框架分析(三)
作者:wdy http://hi.baidu.com/delphiss/blog/item/357663d152c0aa85a1ec9c44.html Yii应用的入口脚本引用出了Yii类,Yii类的 ...
- poj 1193 内存分配
好麻烦的模拟题,一次性过了就好!!!不过用了两天哦.. 小伙伴们慢慢做哦. #include <iostream> #include <list> #include <q ...
- css写法效率问题
这篇文章写的很好了,其实大多数很牛的博客,都是对国外文献的翻译,国内文章的好坏,关键取决于翻译者理解和翻译水平. https://developer.mozilla.org/en-US/docs/We ...
- 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.2
作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...
- Day 4 @ RSA Conference Asia Pacific & Japan 2016
09.00 – 09.45 hrs Advanced Malware and the Cloud: The New Concept of 'Attack Fan-out' Krishna Naraya ...
- Android 系统名字、版本、API level的对应关系
从官网上找到的,具体地址是: http://source.android.com/source/build-numbers.html Code name Version API level Lolli ...
- Servlet配置文件
<url-pattern>/servlet/demo</url-pattern> 1.以 / 开头, /代表工程路径:(必须要加 / ) 2.以 * 开头,必须加后缀名 /* ...