PHP设计模式之备忘录模式
概述:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可将该对象恢复到原先保存的状态
个人举例:
1 看书的书签
2 消息盒子
/**
* 发起角色
* 备忘录角色
* 管理备忘录角色
*/
namespace haibao\design\web\view\design;
use haibao\design\web\common\design\memento\Originator;
use haibao\design\web\common\design\memento\Caretaker;
class Memento extends \haibao\design\web\view\Base{
protected function preRender(){
header("Content-type: text/html; charset=utf-8");
$originator = new Originator();
$originator->state = "ON";
$originator->show();
$caretaker = new Caretaker();
$caretaker->setMemento($originator->createMemento());
$originator->state = "OFF";
$originator->show();
$originator->setMemento($caretaker->getMemento());
$originator->show();
}
}
/**
* 管理角色
*/
namespace haibao\design\web\common\design\memento;
class Caretaker{
private $memento;
public function setMemento($memento){
$this->memento = $memento ;
}
public function getMemento(){
return $this->memento;
}
}
/**
* 备忘录角色
*/
namespace haibao\design\web\common\design\memento;
class Mementor{
private $state;
public function __construct($state){
$this->state = $state;
}
public function getState(){
return $this->state;
}
}
/**
* 发起人角色
*/
namespace haibao\design\web\common\design\memento;
class Originator{
public $state;
public function createMemento(){
return new Mementor($this->state);
}
public function setMemento(Mementor $memento){
$this->state = $memento->getState();
}
public function show(){
echo "
State=".$this->state;
}
}
上一篇: 一开始就要设定未来要往管理阶层走
下一篇: PHP仿 个人博客(2) 数据库增添改删