1、模式定义 备忘录模式又叫做快照模式(snapshot)或 token 模式,备忘录模式的用意是在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以在合适的时候将该对象恢复到原先保存的状态。
我们在编程的时候,经常需要保存对象的中间状态,当需要的时候,可以恢复到这个状态。比如,我们使用eclipse进行编程时,假如编写失误(例如不小心误删除了几行代码),我们希望返回删除前的状态,便可以使用ctrl+z来进行返回。这时我们便可以使用备忘录模式来实现。
2、uml类图
备忘录模式所涉及的角色有三个:备忘录(memento)角色、发起人(originator)角色、负责人(caretaker)角色。
这三个角色的职责分别是:
发起人:记录当前时刻的内部状态,负责定义哪些属于备份范围的状态,负责创建和恢复备忘录数据。 备忘录:负责存储发起人对象的内部状态,在需要的时候提供发起人需要的内部状态。 管理角色:对备忘录进行管理,保存和提供备忘录。 3、示例代码 memento.php
state = $statetosave; } /** * @return mixed */ public function getstate() { return $this->state; }}
originator.php
state = $state; } /** * @return memento */ public function getstateasmemento() { // 在memento中必须保存一份隔离的备份 $state = is_object($this->state) ? clone $this->state : $this->state; return new memento($state); } public function restorefrommemento(memento $memento) { $this->state = $memento->getstate(); }}
caretaker.php
history[$id]; } /** * @param memento $state */ public function savetohistory(memento $state) { $this->history[] = $state; } public function runcustomlogic() { $originator = new originator(); //设置状态为state1 $originator->setstate(state1); //设置状态为state2 $originator->setstate(state2); //将state2保存到memento $this->savetohistory($originator->getstateasmemento()); //设置状态为state3 $originator->setstate(state3); //我们可以请求多个备忘录, 然后选择其中一个进行回滚 //保存state3到memento $this->savetohistory($originator->getstateasmemento()); //设置状态为state4 $originator->setstate(state4); $originator->restorefrommemento($this->getfromhistory(1)); //从备忘录恢复后的状态: state3 return $originator->getstateasmemento()->getstate(); }}
4、测试代码 tests/mementotest.php
name = gandalf; // connect originator to character object $originator->setstate($character); // work on the object $character->name = gandalf the grey; // still change something $character->race = maia; // time to save state $snapshot = $originator->getstateasmemento(); // put state to log $caretaker->savetohistory($snapshot); // change something $character->name = sauron; // and again $character->race = ainur; // state inside the originator was equally changed $this->assertattributeequals($character, state, $originator); // time to save another state $snapshot = $originator->getstateasmemento(); // put state to log $caretaker->savetohistory($snapshot); $rollback = $caretaker->getfromhistory(0); // return to first state $originator->restorefrommemento($rollback); // use character from old state $character = $rollback->getstate(); // yes, that what we need $this->assertequals(gandalf the grey, $character->name); // make new changes $character->name = gandalf the white; // and originator linked to actual object again $this->assertattributeequals($character, state, $originator); } public function teststringstate() { $originator = new originator(); $originator->setstate(state1); $this->assertattributeequals(state1, state, $originator); $originator->setstate(state2); $this->assertattributeequals(state2, state, $originator); $snapshot = $originator->getstateasmemento(); $this->assertattributeequals(state2, state, $snapshot); $originator->setstate(state3); $this->assertattributeequals(state3, state, $originator); $originator->restorefrommemento($snapshot); $this->assertattributeequals(state2, state, $originator); } public function testsnapshotisclone() { $originator = new originator(); $object = new \stdclass(); $originator->setstate($object); $snapshot = $originator->getstateasmemento(); $object->new_property = 1; $this->assertattributeequals($object, state, $originator); $this->assertattributenotequals($object, state, $snapshot); $originator->restorefrommemento($snapshot); $this->assertattributenotequals($object, state, $originator); } public function testcanchangeactualstate() { $originator = new originator(); $first_state = new \stdclass(); $originator->setstate($first_state); $snapshot = $originator->getstateasmemento(); $second_state = $snapshot->getstate(); // still actual $first_state->first_property = 1; // just history $second_state->second_property = 2; $this->assertattributeequals($first_state, state, $originator); $this->assertattributenotequals($second_state, state, $originator); $originator->restorefrommemento($snapshot); // now it lost state $first_state->first_property = 11; // must be actual $second_state->second_property = 22; $this->assertattributeequals($second_state, state, $originator); $this->assertattributenotequals($first_state, state, $originator); } public function teststatewithdifferentobjects() { $originator = new originator(); $first = new \stdclass(); $first->data = foo; $originator->setstate($first); $this->assertattributeequals($first, state, $originator); $first_snapshot = $originator->getstateasmemento(); $this->assertattributeequals($first, state, $first_snapshot); $second = new \stdclass(); $second->data = bar; $originator->setstate($second); $this->assertattributeequals($second, state, $originator); $originator->restorefrommemento($first_snapshot); $this->assertattributeequals($first, state, $originator); } public function testcaretaker() { $caretaker = new caretaker(); $memento1 = new memento(foo); $memento2 = new memento(bar); $caretaker->savetohistory($memento1); $caretaker->savetohistory($memento2); $this->assertattributeequals(array($memento1, $memento2), history, $caretaker); $this->assertequals($memento1, $caretaker->getfromhistory(0)); $this->assertequals($memento2, $caretaker->getfromhistory(1)); } public function testcaretakercustomlogic() { $caretaker = new caretaker(); $result = $caretaker->runcustomlogic(); $this->assertequals(state3, $result); }}
5、总结 如果有需要提供回滚操作的需求,使用备忘录模式非常适合,比如数据库的事务操作,文本编辑器的 ctrl+z 恢复等。