Memento - 备忘录模式
定义
一个备忘录Memento是一个对象,它存储还有一个对象在某个瞬间的内部状态,后者称为备忘录的原发器(originator):

class Memento
{
public:
void setState(State* state) { m_state = state; }
State* state() const { return m_state; }
private:
friend class Viewer;
Memento() { }
private:
State* m_state;
};
class Viewer
{
public:
void drawGraphics();
Memento* createMemento();
void setMemento(Memento* memento);
};
Memento* Viewer::createMemento()
{
State* state = getSelfState();
Memento* memento = new Memento();
memento->setState(state);
return memento;
}
void setMemento(Memento* memento)
{
setSelfState(memento->state());
}
class Command
{
public:
void execute();
void unExecute();
private:
Viewer* m_viewer;
std::vector<Memento*> m_mementos;
};
void Command::execute()
{
m_viewer->drawGraphics();
m_mementos.push_back(m_viewer->createMemento());
}
void Command::unExecute()
{
if(!m_memento.isEmpty())
{
m_viewer->setMemento(m_mementos.back());
m_mementso.pop_back();
}
}
- 保持封装,避免了暴露由自身管理但必须存储在自身之外的数据
- 简化了原发器Viewer,把管理存储状态信息的重任交给了Memento
- 使用代价高,可能因为频繁的操作创建了大量的Memento,能够通过顺序存储,下一次仅仅存储上一次保存后改变的信息
Memento - 备忘录模式的更多相关文章
- 设计模式20:Memento 备忘录模式(行为型模式)
Memento 备忘录模式(行为型模式) 对象状态的回溯 对象状态的变化无端,如何回溯.恢复对象在某个点的状态? 动机(Motivation) 在软件构建过程中,某些对象的状态在转换过程中,可能由于某 ...
- C++设计模式-Memento备忘录模式
Memento模式作用:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态. UML图: Originator:负责创建一个备忘录Me ...
- 设计模式(18)--Memento(备忘录模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式. ...
- 18、Memento 备忘录模式
例如:用于记录快照(顺势状态).存盘 1.Memento Memento设计模式是一种软件设计模式,用于将对象回滚到其先前状态.它是行为设计模式的一部分,与算法和对象之间的职责分配有关. 行为模式描述 ...
- C#设计模式之二十二备忘录模式(Memento Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就是对某个对象的状态进行保 ...
- Java 设计模式系列(十八)备忘录模式(Memento)
Java 设计模式系列(十八)备忘录模式(Memento) 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式.备忘录对象是一个用来存储另外一个对象内部状态 ...
- 行为型模式(十) 备忘录模式(Memento)
一.动机(Motivate) 我们看上图,一个对象肯定会有很多状态,这些状态肯定会相互转变而促进对象的发展,如果要想在某一时刻把当前对象回复到以前某一时刻的状态,这个情况用"备忘录模式&qu ...
- C#设计模式之二十二备忘录模式(Memeto Pattern)【行为型】
一.引言 今天我们开始讲"行为型"设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就 ...
- 十八、Memento 备忘录设计模式
原理: 代码清单: Memento public class Memento { int mondey; ArrayList fruits; Memento(int mondey){ this.mon ...
随机推荐
- chomp成功的返回值是1,chomp对参数去回车符后会改变参数的值,是传入又是传出参数。$arrow_notation = ( chomp( $unpackeing = <STDIN>) );
44 my $unpackeing; 45 my $arrow_notation = ''; 46 print "Enter name to query, enter ex ...
- 第2节 mapreduce深入学习:4, 5
第2节 mapreduce深入学习:4.mapreduce的序列化以及自定义排序 序列化(Serialization)是指把结构化对象转化为字节流. 反序列化(Deserialization)是序列化 ...
- AutoEncoders变种
目录 PCA V.S. Auto-Encoders Denoising AutoEncoders Dropout AutoEncoders PCA V.S. Auto-Encoders deep au ...
- python TCP协议与UDP协议
1. TCP协议 / UDP协议 1.1 TCP协议 1.可靠.慢.全双工通信 2.建立连接的时候 : 三次握手 3.断开连接的时候 : 四次挥手 4.在建立起连接之后 发送的每一条信息都有回执 为了 ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- unity 菜单栏添加新菜单
using UnityEngine; using System.Collections; using UnityEditor; public class jqmTools : CreateSphere ...
- BNUOJ 5997 Fibonacci again and again
Fibonacci again and again Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HD ...
- FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
FIRST集合.FOLLOW集合.SELECT集合以及预测分析表地构造 FIRST集合的简单理解就是推导出的字符串的开头终结符的集合. FOLLOW集合简单的理解就对于非终结符后面接的第一个终结符. ...
- 九度oj 题目1075:斐波那契数列
题目1075:斐波那契数列 时间限制:5 秒 内存限制:32 兆 特殊判题:否 提交:3641 解决:2100 题目描述: 编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的 ...