Learning php design patterns php implementation memorandum patterns (Memento)

Source: Internet
Author: User
This article mainly introduces the memorandum mode in the php design mode and uses php to implement the memorandum mode. if you are interested, refer to it. I. intention
Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state later.

II. memorandum mode structure

III. main roles in memorandum mode
1. Memento role:
The internal status of the initiator object. the initiator determines the internal status of the initiator based on the requirements.
A memorandum can protect its content from being read by any object other than the initiator object.
2. initiator role:
Create a memo object containing the current internal status
Use the memo object to store its internal status
3. Caretaker role:
Saves the object of the memorandum without checking the content of the object.
IV. Advantages and disadvantages of the memorandum model
Memorandum modeAdvantages:
1. sometimes some internal information of the initiator object must be stored outside the initiator object, but it must be read by the initiator object itself.
2. simplified the inator class. The initiators no longer need to manage and save versions of their internal states. The client can manage the versions of these states as needed.
3. when the status of the initiator role changes, it may be invalid. in this case, you can use the currently stored memo to restore the status.
Memorandum modeDisadvantages:
1. if the status of the initiator role needs to be completely stored in the memorandum object, the memorandum object will be expensive in terms of resource consumption.
2. when the owner role stores a memorandum, the owner may not know how much storage space it will occupy, and thus cannot remind the user whether an operation will be expensive.
3. when the status of the initiator role changes, it may be invalid.
V. applicable scenarios of the memorandum model
1. you must save the (partial) state of an object at a certain time point so that it can be restored to the previous state only when necessary.
2. if an interface is used to directly obtain these states for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.
VI. memorandum mode and other modes
1. command mode (Command mode): The Command mode can also be used to restore the object state. Generally, the command mode supports multi-level state rollback. Memento is only a simple restoration (snapshot ). In each undo in Command mode, Memento can be used to save the object state.
2. Iterator mode (Iterator mode): Memorandum can be used for iteration
VII. memorandum mode PHP example

<? Php/*** Originator role */class Originator {private $ _ state; public function _ construct () {$ this-> _ state = '';} /*** create a memorandum ** @ return Memento the object containing the current state */public function createMemento () {return new Memento ($ this-> _ state );} /*** restore the initiator to the recorded State of the memorandum object * @ param Memento $ memento */public function restoremento (Memento $ memento) {$ this-> _ state = $ memento-> getState ();} public function setState ($ state) {$ this-> _ state = $ state ;} public function getState () {return $ this-> _ state;}/*** test method, display Status */public function showState () {echo "Original Status :", $ this-> getState (),"
";}}/*** 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 $ me Mento) {$ this-> _ memento = $ memento;}/*** Client */class Client {/*** Main program. */public static function main () {/* create the target object */$ org = new Originator (); $ org-> setState ('open '); $ org-> showState ();/* create memo */$ memento = $ org-> createMemento (); /* save this memo through Caretaker */$ caretaker = new Caretaker (); $ caretaker-> setMemento ($ memento ); /* change the status of the target object */$ org-> setState ('close'); $ org-> showState ();/* also Original operation */$ org-> restoreMemento ($ caretaker-> getMemento (); $ org-> showState () ;}} Client: main ();?>

The above is the code that uses php to implement the memorandum mode. There are also some concepts about the memorandum mode, which will be helpful for your learning.

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.