Learn PHP Design Patterns PHP Implementation Memo mode (Memento) _php tips

Source: Internet
Author: User

I. Intention
captures the internal state of an object without damaging the encapsulation, and saves the state outside the object. This allows the state of the object to revert to its previously saved state later.

Second, the memorandum pattern structure diagram

iii. key roles in the memo model
1, Memo (Memento) role:
The internal state of the storage initiator (originator) object, and the initiator determines the internal state of the memo storage initiator as needed.
A memo protects its content from being read by any object other than the initiator (originator) object.
2, promoter (originator) Role:
Create a Memo object that contains the current internal state
Use a Memo object to store its internal state
3, the person in charge (caretaker) role:
Responsible for saving the memo object without checking the contents of the Memo object
Iv. Advantages and disadvantages of the memo model
Memo mode ofAdvantages:
1. Sometimes the internal information of some initiator objects must be kept outside the initiator object, but must be read by the initiator object itself.
2, simplified the initiator (originator) class. The initiator (originator) no longer needs to manage and save versions of its internal state, and the client can manage the versions of those States that they need
3, when the status of the promoter role changes, it is possible that this state is invalid, this time can be used to temporarily save the memo to restore the state.
Memo mode ofDisadvantages:
1. If the status of the initiator role needs to be stored completely in the memo object, the memo object will be expensive on resource consumption.
2. When the person responsible for the role of a memo stored, the owner may not know how much storage space this state will occupy, thus unable to remind users whether an operation is expensive.
3, when the status of the promoter role changes, it is possible that this state is invalid.
v. Application of Memo Mode
1, you must save an object at a certain time (part) state, so that it can revert to the previous state when needed.
2, if one uses the interface to let other objects get these states directly, it will expose the implementation details of the object and destroy the encapsulation of the object.
Vi. Memo mode and other modes
1. Command mode (commanding mode): The commands mode can also be used to restore the state of the object, and the General Command mode can support multiple-state rollback, memento just a simple recovery (snapshot). In each undo of command mode, you can use memento to save the state of an object.
2, iterator mode (iterator mode): Memos can be used for iterations
Seven, Memo mode PHP example

<?php/** * Promoter (originator) ROLE * * class Originator {private $_state;
  Public Function __construct () {$this->_state = '; /** * Create Memo * @return Memento the Memo object containing the current state/Public function Creatememento () {return new Memento ($t
  His->_state);
    /** * Restores the initiator to the status of the Memo object record * @param Memento $memento/Public Function Restorememento (Memento $memento) {
  $this->_state = $memento->getstate ();
  The Public Function SetState ($state) {$this->_state = $state;
  The Public Function getState () {return $this->_state;  /** * Test method, display status/Public function showstate () {echo "Original status:", $this->getstate (), "<br
  /> ";
 
  }/** * Memo (Memento) Role/class Memento {private $_state;
  Public function __construct ($state) {$this->setstate ($state);
  The Public Function getState () {return $this->_state; The Public Function SetState ($state) {$this->_sTate = $state;
 
  }/** * Owner (caretaker) role/class caretaker {private $_memento;
  Public Function Getmemento () {return $this->_memento;
  The Public Function Setmemento (Memento $memento) {$this->_memento = $memento;
   }/** * Client/class Client {/** * Main program.
    */public static function main () {*/* Create target object */$org = new originator ();
    $org->setstate (' open ');
 
    $org->showstate ();
 
    /* Create a memo */$memento = $org->creatememento ();
    * * Save this memo via caretaker/$caretaker = new Caretaker ();
 
    $caretaker->setmemento ($memento);
    /* Change the status of the target object * * * $org->setstate (' close ');
 
    $org->showstate ();
    /* Restore operation * * $org->restorememento ($caretaker->getmemento ());
  $org->showstate (); } client::main ();?>

The above is the use of PHP implementation of the memo mode code, there are some of the concept of memo mode of separation, I hope to help you learn.

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: info-contact@alibabacloud.com 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.