注册 登录  
 加关注

网易博客网站关停、迁移的公告:

将从2018年11月30日00:00起正式停止网易博客运营
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

php开发lnmp

《西安--木木》-经历丰富了生活。 架构师QQ群: 246695517

 
 
 

日志

 
 

Memento备忘录模式  

2018-03-12 14:55:02|  分类: 设计模式--php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


<?php
/**
* Memento备忘录模式
*
* 发起人:记录当前时刻的内部状态,负责定义哪些属于备份范围的状态,负责创建和恢复备忘录数据。
* 备忘录:负责存储发起人对象的内部状态,在需要的时候提供发起人需要的内部状态。
* 管理角色:对备忘录进行管理,保存和提供备忘录。
*/

/**
* 多步悔棋功能,案例在线下象棋
*/

//原发器 下象棋类
class Originator
{
private $chessman = '';//棋名称,兵
private $x = 0; //x坐标
private $y = 0; //y坐标

public function __construct($chessman, $x, $y)
{
$this->chessman = $chessman;
$this->x = $x;
$this->y = $y;
}

public function setChessman($chessman)
{
$this->chessman = $chessman;
}

public function setX($x)
{
$this->x = $x;
}

public function setY($y)
{
$this->y = $y;
}

public function getChessman()
{
return $this->chessman;
}

public function getX()
{
return $this->x;
}

public function getY()
{
return $this->y;
}

//保存状态
public function saveChessmanStatus()
{
return new Memento($this->chessman, $this->x, $this->y);
}

//恢复状态
public function restoreChessmanStatus(Memento $memento)
{
$this->chessman = $memento->getChessman();
$this->x = $memento->getX();
$this->y = $memento->getY();
}


}

//备忘录
class Memento
{
private $chessman = '';//棋名称,兵
private $x = 0; //x坐标
private $y = 0; //y坐标

public function __construct($chessman, $x, $y)
{
$this->chessman = $chessman;
$this->x = $x;
$this->y = $y;
}

public function setChessman($chessman)
{
$this->chessman = $chessman;
}

public function setX($x)
{
$this->x = $x;
}

public function setY($y)
{
$this->y = $y;
}

public function getChessman()
{
return $this->chessman;
}

public function getX()
{
return $this->x;
}

public function getY()
{
return $this->y;
}

}

//负责人
class Caretaker
{

private $mementolist = array();
private $active_index = -1;
public $current_index = -1;

public function getMemento($i)
{
if($i<0 || $i>$this->active_index){
return false;
}
return $this->mementolist[$i];
}

public function setMemento($v)
{
//$arg_list = func_num_args();
if($v instanceof Memento){
if($this->current_index < $this->active_index){
//当前位置对象
$val = $this->getMemento($this->current_index);
$this->active_index += 2;
$this->current_index = $this->active_index;
return array_push($this->mementolist,$val,$v);
}else{
$this->active_index++;
$this->current_index++;
return array_push($this->mementolist,$v);
}

}

}
}




//客户端代码
$chess = new Originator('兵',6,6);
echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";
$c = new Caretaker();
$c->setMemento($chess->saveChessmanStatus());
echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";

$chess ->setX(7);
$c->setMemento($chess->saveChessmanStatus());
echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";

$chess ->setY(5);
$c->setMemento($chess->saveChessmanStatus());
echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";
//悔棋
echo '悔棋2步', '<br/>';
echo $c->current_index.'<br/>';
$c ->current_index= $c->current_index-1;
$c ->current_index= $c->current_index-1;
echo $c->current_index.'<br/>';
$chess->restoreChessmanStatus($c->getMemento($c->current_index));

echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";
//走一步

$chess ->setX(8);
$c->setMemento($chess->saveChessmanStatus());

echo "棋子" . $chess->getChessman() . "当前位置为:" . "第" . $chess->getX() . "行" . "第" . $chess->getY() . "列<br/>";


  评论这张
 
阅读(29)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018