标签:
<?php class Originator { private $state; public function __set($param, $value) { if ($param == ‘state‘) { $this->state = $value; } } public function __get($param) { if ($param == ‘state‘) { return $this->state; } } public function create_memento() : Memento { return (new Memento($this->state)); } public function set_memento(Memento $memento) { $this->state = $memento->state; } public function show() { echo "state:" . $this->state ."<br/>"; } } class Memento { private $state; public function __construct(string $state) { $this->state = $state; } public function __get($param) { if ($param == ‘state‘) { return $this->state; } } } class Caretaker { private $memento; public function __set($param, $value) { if ($param == ‘memento‘) { $this->memento= $value; } } public function __get($param) { if ($param == ‘memento‘) { return $this->memento; } } } //client CODE $o = new Originator(); $o->state = ‘ON‘; $o->show(); $c = new Caretaker(); $c->memento = $o->create_memento(); $o->state = ‘OFF‘; $o->show(); $o->set_memento($c->memento); $o->show();
备忘录模式:
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
使用备忘录模式可以把复杂的对象内部信息对其他的对象屏蔽起来。
标签:
原文地址:http://www.cnblogs.com/wy0314/p/4779434.html