<?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);