Memo mode is a behavioral pattern that captures the internal state of an object without breaking the encapsulation, and saves the state outside that object. This allows you to revert the state of the object to a previously saved state later.
Main role
Memo (Memento) role: The internal state of the storage initiator (originator) object, and the initiator determines which internal states of the memo store initiator as needed. A memo can protect its contents from being read by any object other than the initiator (originator) object.
Initiator (originator) Role: Create a Memo object containing the current internal state, using the memo object to store its internal state
Person in charge (caretaker) role: Responsible for saving memo objects without checking the contents of Memo objects
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.
Class diagram
instance
<?phpclass Originator {//initiator (originator) role private $_state; Public Function __construct () {$this->_state = '; } public Function Creatememento () {//Create Memo return new Memento ($this->_state); The Public Function Restorememento (Memento $memento) {//restores the initiator to the state of the memo object record $this->_state = $memento->getst Ate (); The Public Function setState ($state) {$this->_state = $state;} Public Function GetState () {return $this->_state;} Public Function Showstate () {}}class Memento {//Memo (Memento) role private $_state; Public function __construct ($state) {$this->setstate ($state); } public Function GetState () {return $this->_state;} Public Function SetState ($state) {$this->_state = $state;}} Class Caretaker {//person in charge (caretaker) role private $_memento; Public Function Getmemento () {return $this->_memento;} Public Function Setmemento (Memento $memento) {$this->_memento = $memento;}} //client/* Create target object */$org = new originator (); $org->setstate (' open '); $org->showstate ();/* Create 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 ();? >
Advantages and Disadvantages
Advantages
Sometimes the internal information of some initiator objects must be stored outside the initiator object, but must be read by the initiator object itself.
simplifies the initiator (originator) class. The initiator (originator) no longer needs to manage and save a version of its internal state, and clients can manage the versions of those states they need.
When the status of the initiator is changed, it is possible that the state is invalid, and then the state can be restored using a temporarily stored memo.
Disadvantages
If the status of the initiator role needs to be stored completely in the memo object, the memo object can be expensive on resource consumption.
When the responsible role stores a memo, the person in charge may not know how much storage space the state consumes, and thus cannot alert the user whether an operation is expensive.
When the status of the initiator role changes, it is possible that the state is invalid.