PHP implementation of design mode memo mode

Source: Internet
Author: User

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.