Home>

I. Intent

Without compromising encapsulation,Capture the internal state of an object,And save this state outside the object.This can later restore the state of the object to the previously saved state.

Structure chart of memo mode

Third, the main role in the memo mode1. Memento role:

Stores the internal state of the originator object,The initiator decides which internal state of the initiator the memo stores as needed.

The memo can protect its content from being read by any object other than the originator object.

2. Originator role:Create a memo object with the current internal state

Use a memo object to store its internal state

3. The role of caretaker:Responsible for saving memo objects,Do not check the contents of the memo object

Advantages and disadvantages of the memo mode

in memo modeadvantage:1. Sometimes the internal information of some sponsor objects must be saved outside the sponsor objects,But it must be read by the initiator object itself.

2. Simplified the originator class. The originator no longer needs to manage and save versions of its internal state,Clients can manage the versions of these states they need

3. When the status of the initiator role changes,It is possible that this status is invalid.At this time, you can use the temporarily stored memo to restore the status.

in memo modeDisadvantages:1. If the status of the initiator role needs to be completely stored in the memo object,Then the memo object will be expensive in terms of resource consumption.

2. When the person in charge stores a memo,The person in charge may not know how much storage space this state will occupy,Therefore, it is impossible to remind the user whether an operation will be expensive.

3. When the status of the initiator role changes,It is possible that this status is invalid.

V. Applicable scenarios of memo mode1. You must save the (partial) state of an object at a certain moment, so that it can be restored to its previous state when needed later.

2. If an interface is used by other objects to get these states directly,Will expose the implementation details of the object and destroy the object's encapsulation.

Six, memo mode and other modes1, command mode (command mode):command mode can also be used to restore the state of the object,The general command mode can support multi-level state rollback.memento is just a simple recovery (snapshot). In each undo in command mode, you can use memento to save the state of the object.

2. Iterator mode (iterator mode):memos can be used for iteration

Example of php in memo mode

<?php
/**
 * Originator role
 * /
class originator {
  private $_state;
  public function __construct () {
    $this->_state="";
  }
  /**
   * Create memo
   * @return memento memo object containing current status
   * /
  public function creatememento () {
    return new memento ($this->_ state);
  }
  /**
   * Restore the originator to the state of the memo object record
   * @param memento $memento
   * /
  public function restorememento (memento $memento) {
    $this->_state=$memento->getstate ();
  }
  public function setstate ($state) {
    $this->_state=$state;
  }
  public function getstate () {
    return $this->_state;
  }
  /**
   * Test method,Display state
   * /
  public function showstate () {
    echo "original status:", $this->getstate (), "<br />";
  }
}
/**
 * Memento role
 * /
class memento {
  private $_state;
  public function __construct ($state) {
    $this->setstate ($state);
  }
  public function getstate () {
    return $this->_state;
  }
  public function setstate ($state) {
    $this->_state=$state;
  }
}
/**
 * Caretaker role
 * /
class caretaker {
  private $_memento;
  public function getmemento () {
    return $this->_memento;
  }
  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 note * /
    $memento=$org->creatememento ();
    /* Save this note 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 ();
?>
php
  • Previous Tutorial for Creating an Incremental Sequence in MySQL
  • Next JS implementation of string to date and comparison size example analysis