一天一个设计模式(16)——备忘录模式
备忘录模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php class Memento{ private $_state; public function __construct($state){ $this->_state=$state; } public function getState(){ return $this->_state; } } class MementoManager{ private $_mementoList=[]; public function add($memento){ $this->_mementoList[]=$memento; } public function get($index){ return $this->_mementoList[$index]; } } class Demo{ public $state; public function save(){ return new Memento($this->state); } public function get($memento){ return $memento->getState(); } public function set($memento){ $this->state=$memento->getState(); } } $demo=new Demo(); $manager=new MementoManager(); $demo->state='state1'; $manager->add($demo->save()); $demo->state='state2'; $demo->state='state3'; $manager->add($demo->save()); $demo->state='state4'; echo $demo->state.PHP_EOL; $demo->set($manager->get(1)); echo $demo->state.PHP_EOL; $demo->set($manager->get(0)); echo $demo->state.PHP_EOL;
本文来自博客园,作者:Bin_x,转载请注明原文链接:https://www.cnblogs.com/Bin-x/p/7363463.html