B9:备忘录模式 Memento
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态
UML:

示例代码:
class Role
{
private $hp;
private $attack; public function __construct($hp, $attack)
{
$this->hp = $hp;
$this->attack = $attack;
} public function fight()
{
$this->hp = 0;
echo "开始打Boos,血条到了{$this->hp}" . PHP_EOL;
} // 创建一个备份
public function createMemento()
{
return new RoleMemento($this->hp, $this->attack);
} public function restoreMemento(RoleMemento $memento)
{
$this->hp = $memento->getHp();
$this->attack = $memento->getAttack();
echo "又恢复了, 血条到了{$this->hp};攻击力到了{$this->attack}";
}
} class RoleMemento
{
private $hp;
private $attack; public function __construct($hp, $attack)
{
$this->hp = $hp;
$this->attack = $attack;
} public function __call($name, $args)
{
$name = strtolower(str_replace('get', '', $name));
if (property_exists($this, $name)) {
return $this->$name;
}
}
} class MementoAdmin
{
private $memento; public function setMemento(RoleMemento $roleMemento)
{
$this->memento = $roleMemento;
} public function getMemento()
{
return $this->memento;
}
} $role = new Role(5900, 1500); $admin = new MementoAdmin();
$admin->setMemento($role->createMemento()); // 创建一个备份 $role->fight(); $role->restoreMemento($admin->getMemento());
如果只有一个备忘录,可以取消备忘录管理者.
B9:备忘录模式 Memento的更多相关文章
- 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)
原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...
- 备忘录模式 Memento 快照模式 标记Token模式 行为型 设计模式(二十二)
备忘录模式 Memento 沿着脚印,走过你来时的路,回到原点. 苦海翻起爱恨 在世间难逃避命运 相亲竟不可接近 或我应该相信是缘份 一首<一生所爱>触动了多少 ...
- 备忘录模式-Memento Pattern(Java实现)
备忘录模式-Memento Pattern Memento备忘录设计模式是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到以前保存的状态. 本文中的场景: 有一款游戏可以随时存档, ...
- 21备忘录模式Memento
一.什么是备忘录模式 Memento模式也叫备忘录模式,是行为模式之 一,它的作用是保存对象的内部状态,并在需要 的时候(undo/rollback)恢复对象以前的状态. 二.备忘录模式的应用场景 如 ...
- Java 设计模式系列(十八)备忘录模式(Memento)
Java 设计模式系列(十八)备忘录模式(Memento) 备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式.备忘录对象是一个用来存储另外一个对象内部状态 ...
- 二十四种设计模式:备忘录模式(Memento Pattern)
备忘录模式(Memento Pattern) 介绍在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到保存的状态. 示例有一个Message实体类,某 ...
- 设计模式之备忘录模式(Memento)
备忘录模式(Memento) 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. Originator(发起人):负责创建一个备忘录 ...
- c++ 行为型_备忘录模式(Memento)
行为型_备忘录模式(Memento) 作用场景: 当意图在对象外面保存对象的内部状态,但是又不想破坏对象的封装性,就可以考虑备忘录模式. 解释: 其参与者包括 1.Memnto(备忘录,如下列Coun ...
- 23.备忘录模式(Memento Pattern)
using System; using System.Collections.Generic; namespace ConsoleApplication6 { /// <summary> ...
随机推荐
- eclipse启动几秒后报错 (一闪而过)
eclipse启动报错,让查看.metadata/.log日志 1 !SESSION 2013-09-23 17:28:28.484 ------------------------------- ...
- delphi dispose释放内存的方法 New 和 GetMem 的区别
来自:http://blog.sina.com.cn/s/blog_4bc47d2301018trf.html -------------------------------------------- ...
- solr params.json
The Request Parameters API allows creating parameter sets that can override or take the place of par ...
- lucene5 实时搜索
openIfChanged public static DirectoryReader openIfChanged(DirectoryReader oldReader) throws IOExcept ...
- 修改hadoop的jar包运行时候分配的jvm内存
在hadoop-env.sh中修改参数添加 export HADOOP_HEAPSIZE="4096" 设置分配的最大jvm内存为4096,一般用于jar包里面除了执行map和re ...
- ZCMU新人训练赛-B
Tom's Meadow Tom has a meadow in his garden. He divides it into N * M squares. Initially all the sq ...
- 模板—字符串—Manacher
模板—字符串—Manacher Code: #include <cstdio> #include <cstring> #include <algorithm> us ...
- 2016noipday1t1玩具迷题结题报告
经常读这个代码有益于比赛时想起一些思路.... day1t1,洛谷dalao称之为水题...??然后我去年还是没拿到分,就这个,我还就写了40%的数据,AC到40,然而这不是关键,注释了freopen ...
- UVA548 Tree (二叉树的遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 4、Django实战第4天:xadmin快速搭建后台管理系统
Django默认为我们提供了后台管理系统admin, urls.py中配置的第一条就是访问后台管理系统admin的 urlpatterns = [ url(r'^admin/', admin.site ...