百度词条的解释
http://baike.baidu.com/view/3591132.htm
<?php
//发起人类Originator
class Originator {
private $state;
public function __set($key, $value) {
$this->$key = $value;
}
public function __get($key) {
if(isset($this->$key)) {
return $this->$key;
} else {
return NULL;
}
}
//创建备忘录,将当前要保存的信息导入并实例化备忘录
public function createMemento() {
return new Memento($this->state);
}
//恢复备忘录,将Memento导入并将相关数据恢复
public function setMemento(Memento $memento) {
$this->state = $memento->state;
}
//展示状态数据
public function show() {
echo '当前状态是:'.$this->state;
}
}
//备忘录类Memento
class Memento {
private $state;
public function __set($key, $value) {
$this->$key = $value;
}
public function __get($key) {
if(isset($this->$key)) {
return $this->$key;
} else {
return NULL;
}
}
public function __construct($state) {
$this->state = $state;
}
}
//管理者类Caretaker
class Caretaker {
private $memento;
public function __set($key, $value) {
$this->$key = $value;
}
public function __get($key) {
if(isset($this->$key)) {
return $this->$key;
} else {
return NULL;
}
}
}
$o = new Originator();
//初始状态为On
$o->state = 'On';
$o->show();
//创建备忘录并保存状态
$caretaker = new Caretaker();
$caretaker->memento = $o->createMemento();
//更改Originator状态 = Off
$o->state = 'Off';
$o->show();
//恢复到原始状态
$o->setMemento($caretaker->memento);
$o->show();
?>