Memorandum mode and php implementation

Source: Internet
Author: User
The memo mode is a behavior mode. it captures the internal state of an object without damaging encapsulation and saves the state outside the object. In this way, the object state can be restored to the previously saved State Memorandum mode in the future. it is a behavior pattern, without compromising encapsulation, capture the internal state of an object and save the state outside the object. In this way, the object can be restored to the previously saved state later.

Primary role

Memento role: stores the internal status of the initiator object, and the initiator determines the internal status of the initiator according to the needs. A memorandum can protect its content from being read by any object other than the initiator object.

Initiator role: Creates a memorandum object that contains the current internal status, and stores its internal status using the memorandum object.

The owner (Caretaker) role is responsible for saving the object of the memorandum without checking the content of the object.

Applicability

You must save the (partial) state of an object at a certain time point so that it can be restored to the previous state later.

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.

Class diagram

Instance

 _ State = '';} public function createMemento () {// Create a memorandum return new Memento ($ this-> _ state);} public function restoremento (Memento $ memento) {// restore the initiator to the state recorded by the memorandum object $ this-> _ state = $ memento-> getState ();} public function setState ($ state) {$ this-> _ state = $ state;} public function getState () {return $ this-> _ state;} public function showState () {}} class Memento {// memorandum (Memento) role private $ _ sta Te; public function _ construct ($ state) {$ this-> setState ($ state);} public function getState () {return $ this-> _ state ;} public function setState ($ state) {$ this-> _ state = $ state;} class Caretaker {// Caretaker role private $ _ memento; public function getMemento () {return $ this-> _ memento;} public function setMemento (Memento $ memento) {$ this-> _ memento = $ memento ;}} // client/* create the target object */$ org = New Originator (); $ org-> setState ('open'); $ org-> showState (); /* Create a memo */$ memento = $ org-> createMemento ();/* save the memo through 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 some internal information of the initiator object must be stored outside the initiator object, but it must be read by the initiator object itself.

Simplifies 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.

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.

Disadvantages

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.

When the owner role stores a memorandum, the owner may not know how much storage space the state will occupy, and thus cannot remind the user whether an operation will be expensive.

When the status of the initiator role changes, it may be invalid.

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.