【设计模式】行为型07备忘录模式(Memento Pattern)
参考地址:http://www.runoob.com/design-pattern/memento-pattern.html
对原文总结调整,以及修改代码以更清晰的展示:
备忘录模式(快照模式):
定义:顾名思义,备忘录模式是在不破坏封装性的前提下,通过备忘录对象,记录一个对象的内部状态,并在该对象之外保存这个状态。
应用场景: 2、打游戏时的存档。 3、Windows 里的 ctri + z。 4、IE 中的后退。 4、数据库的事务管理。
图:日后再画
代码:
1、状态记录类:
package com.pat.snap;
/**
* 备忘录对象,用于记录
* @author Administrator
*
*/
public class Memento {
private String text="";
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
2、编辑器-被记录类
package com.pat.snap;
/**
* 记事本对象,被记录的对象
* @author Administrator
*
*/
public class TextEditor {
//正在编辑的内容
private String text="";
public String getText() {
return text;
}
public void setText(String text) {
this.text = this.text+text;
}
//保存
public Memento saveHistory() {
Memento curr = new Memento();
curr.setText(this.text);
return curr;
}
//ctrl+z
public void backUp(Memento mem) {
this.text=mem.getText();
}
}
3、存储记录
package com.pat.snap;
import java.util.ArrayList;
import java.util.List;
/**
* 管理历史,负责恢复
* @author Administrator
*
*/
public class CareTaker {
private List<Memento> mementoList = new ArrayList<Memento>();
public void add(Memento state){
mementoList.add(state);
}
public Memento get(int index){
return mementoList.get(index);
}
}
4、测试类
package com.pat.snap;
/**
* 测试类
* @author ZX
*
*/
public class Test {
public static void main(String[] args) {
//创建历史记录管理类
CareTaker ct =new CareTaker();
//记事本编辑文件
TextEditor td = new TextEditor();
td.setText("123");
td.setText("456");
//创建快照1
Memento h1 = td.saveHistory();
ct.add(h1);
td.setText("789");
td.setText("10");
td.setText("11");
//创建快照2
Memento h2 = td.saveHistory();
ct.add(h2);
//获取快照1
Memento memento1 = ct.get(0);
System.out.println(memento1.getText());
//获取快照2
Memento memento2 = ct.get(1);
System.out.println(memento2.getText());
}
}
5、运行结果:
123456
1234567891011
【设计模式】行为型07备忘录模式(Memento Pattern)的更多相关文章
- 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
- 二十四种设计模式:备忘录模式(Memento Pattern)
备忘录模式(Memento Pattern) 介绍在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到保存的状态. 示例有一个Message实体类,某 ...
- 备忘录模式-Memento Pattern(Java实现)
备忘录模式-Memento Pattern Memento备忘录设计模式是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到以前保存的状态. 本文中的场景: 有一款游戏可以随时存档, ...
- c++ 行为型_备忘录模式(Memento)
行为型_备忘录模式(Memento) 作用场景: 当意图在对象外面保存对象的内部状态,但是又不想破坏对象的封装性,就可以考虑备忘录模式. 解释: 其参与者包括 1.Memnto(备忘录,如下列Coun ...
- [设计模式] 18 备忘录模式Memento Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对备忘录模式是这样说的:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存 ...
- 23.备忘录模式(Memento Pattern)
using System; using System.Collections.Generic; namespace ConsoleApplication6 { /// <summary> ...
- 备忘录模式-Memento Pattern
1.主要优点 备忘录模式的主要优点如下: (1)它提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原. (2) ...
- 用最简单的例子理解备忘录模式(Memento Pattern)
简单来说,备忘录模式就是支持回退操作.假设让一个Notepad支持回退操作,如何实现呢? 首先需要一个备忘录类. public class Memento { private string _msg; ...
- php备忘录模式(memento pattern)
晚上刷起来. <?php /* The memento pattern provides the object restore functionality. Implementation is ...
随机推荐
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
原文:Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定) ------------------------------ ...
- PD生成兼容Oracle、Mysql脚本
mysql date 改为 datetime ->运行sql脚本 Oracle " clustered " 替换为空,即key(XX) ->运行sql脚本
- linux_crontab_定时删除
#每天2:30 删除4天前qac的原始log30 2 * * * find /home/iknow/ETL/RetlPull/retl-pull/data/qac -name qac.log.new. ...
- Oracle 已有则更新,没有则插入
使用merge merge into 表名 t1 using (select '数据数据' 字段1,'数据数据' 字段2 from dual) t2 on (t1.字段1 = t2.字段1) when ...
- 【C#】【WPF】如何读写app.config文件
WPF生成的项目中会有.exe.config.一般是系统默认配置的 格式是xml格式,C#的项目可以直接读写这些文件.方法代码如下. public static string GetConnectio ...
- 2-18-搭建mysql集群实现高可用
1 环境清理以及安装 1.1 mysql旧版本清除 准备5台虚拟机,分配如下 mysql管理结点:xuegod1.cn IP:192.168.10.31 (安装server.clien ...
- WPF DataGrid支持的列类型
WPF DataGrid支持下面几种列类型: DataGridTextColumn DataGridCheckBoxColumn DataGridComboBoxColumn DataGridHype ...
- js 点击超链接,执行js脚本,而不进行url跳转
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- SQLServer2008-2012开启远程连接的配置方法
一.远程连接端口设置(很关键的一步)1.在服务器上打开SQL Server Configuration Manager.选择SQL Server配置管理器->SQL Server 网络配置-&g ...
- mingw 构建 mysql-connector-c-6.1.9记录(26种不同的编译错误,甚至做了一个windows系统返回错误码与System V错误码的一个对照表)
http://www.cnblogs.com/oloroso/p/6867162.html