Memo mode:
Also known as snapshot mode or token mode, the internal state of an object is captured and stored outside the object without breaking the enclosing condition. The object can then be restored to its previously saved state.
Role:
1. Creator: Responsible for creating a memo that records the internal state of the current moment, and can use the memo to restore the internal state. The initiator can decide what internal state the memo stores itself as needed.
2. Memo: Responsible for storing the internal state of the initiator object and to prevent access to the memo from objects other than the initiator. The memo has two interfaces: the manager can only see the narrow interface of the memo, and he can only pass the memo to other objects. The initiator can see the wide interface of the memo, allowing it to access all the data needed to return to the previous state.
3. Manager: Responsible for accessing the memo, cannot access or operate the content.
UML Class Diagrams:
Applicability:
You must save the (partial) state of an object at a certain point in time so that it can revert back to its previous state if needed later.
If one uses interfaces to get these states directly from other objects, it exposes the implementation details of the object and destroys the encapsulation of the object.
Code implementation:
id = $id; $this->name = $name; $this->livelevel = $liveLevel; }}//Memo Manager Class originator{public static $mementos = Array (); private static $instance = null; The singleton mode ensures that only one manager private function __construct () {}//returns a singleton object static function getinstance () {if (! Self:: $instance instanceof Slef)} {self:: $instance = new self (); } return Self:: $instance; }//Save Memo function Setmemento ($id, Memento $memento) {self:: $mementos [$id] = $memento; }//Fetch Memo function Getmemento ($id) {return self:: $mementos [$id]; }}//Creator, player, can access its own status class player{private static $i = 0; Static variable summation is used to assign public $id to $id; Each object is unique and is used to save the state memo to the manager private $name; Name Private $liveLevel; Health value function __construct ($name) {$this->name = $name; $this->id = self:: $i; Self:: $i + +; }//Initialize function init () {$this->livelevel = 100; }//Health value reduced by ten Function damage () {$this->livelevel-=10; }//Show existing status function Displaystate () {echo "name:". $this->name. "
"; echo "Health value:" $this->livelevel. "
"; }//save state into a memo, the memo will be placed in Manager function SaveState () {$originator = Originator::getinstance (); $originator->setmemento ($this->id,new Memento ($this->id, $this->name, $this->livelevel)); }//Recovery Memo function GetState () {$originator = Originator::getinstance (); $memento = $originator->getmemento ($this->id); $this->id = $memento->id; $this->name = $memento->name; $this->livelevel = $memento->livelevel; }}//test//Create, Initialize role and display status $player = new Player ("Zhang San"); $player->init (); $player->displaystate (); The health value 100//starts the game before the archive $player->savestate ();//Start the game, receive damage health decrease, show status $player->damage (); $player->damage (); $ Player->displaystate (); Health value 80//return to the original state, showing the status $player->getstate (); $player->displaystate (); Health value of?>