<?php/* Memo mode: Gets the internal state of an object without destroying the package, and saves the state outside the object. This restores the object to its previous state (behavior pattern) * Initiator: Records the internal state of the current moment, defines which state is part of the backup scope, and is responsible for creating and recovering Memo data. * Memo: Responsible for storing the internal state of the initiator object, providing the internal state required by the initiator when needed. * Management Role: Manage memos, save and provide memos. *///initiator, it needs to save its own state, itself contains a memo and recovery operation class Origin{private $state;p ublic function SetState ($state) {$this->state = $ State;} Public function Show () {echo $this->state. <br/> ";} Public Function Setmemento () {//Save to Memo return new Memento ($this->state);} Public function Restore (memento $memento) {//Get the state accessed in the memo, restore $this->state = $memento->getstate ();}} Class memento{//memo, Store State private $state;p ublic function __construct ($state) {$this->state = $state;} Public Function GetState () {return $this->state;}} Class caretaker{//manager, Management memo private $memento;p ublic function Getmemento () {return $this->memento;} Public Function Setmemento ($memento) {$this->memento = $memento;}} $org = new Origin (), $org->setstate (' open '); $org->show (); $memento = $org->setmemento ();//Initiator to save state, generate memo $ persion = new CarEtaker (); $persion->setmemento ($memento); $org->setstate (' close '); $org->show (); $org->restore ($ memento);//Recover $org->show ();? >
UML Class Diagrams:
PHP implementation of design mode memo mode