备忘录模式

<?php
/**
* 备忘录三要素:
* 原发器负责生成备忘录,解析备忘录
* 备忘录负责维护数据
* 负责人负责维护备忘录
* 备忘录优点:提供状态恢复机制
* 缺点:存的备忘录对象过多,消耗系统资源
*/

/**
* 原发器
* Class Handle
*/
class Handle
{
public $x;
public $y;

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

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

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

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

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

//撤销
public function revoke(Param $param){
$this->x = $param->getX();
$this->y = $param->getY();
}

public function save(){
return new Param($this->getX(),$this->getY());
}
}

/**
* 备忘录
* Class Param
*/
class Param{
public $x;
public $y;

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

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

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

function display(Handle $handle){
echo '东经:'.$handle->getX().'度'.',北纬'.$handle->getY().'<br/>';
}

/**
* 负责人
*/
class Charge{
public $paramList = [];

public function setParam($param){
$this->paramList[] = $param;
}

public function getParam(){
return array_pop($this->paramList);
}
}

$charge = new Charge();
$handle = new Handle('33.33','66.66');
display($handle);
$charge->setParam($handle->save());
$handle->setX('44.44');
display($handle);
$charge->setParam($handle->save());
$handle->setX('99.99');
$handle->setY('99.99');
display($handle);
echo '----开启连续性回滚-----'.'<br/>';
$charge->setParam($handle->save());
//开始第一次回滚
$handle->revoke($charge->getParam());
display($handle);
//第二次回滚
$handle->revoke($charge->getParam());
display($handle);
//第三次回滚
$handle->revoke($charge->getParam());
display($handle);

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值