Memorandum mode of PHP design mode

Source: Internet
Author: User
The memorandum mode of PHP design mode the memorandum mode is a behavior mode. what is behavior mode? that is, the responsibility assignment between the behavior mode design and the algorithm and the object not only describes the object or class mode, it also describes the communication modes between them and describes the complex control flows that are difficult to trace during running. They shift your attention from the control flow to the relationship between objects. The behavior-type class mode uses the inheritance mechanism to distribute behavior among classes

Overview: capture the internal state of an object without compromising encapsulation, and save the state outside the object so that the object can be restored to its original state.

Personal example:

1. bookmarking

2 Message box

/**

* Initiate a role

* Memorandum role

* Manage memo roles

*/

Namespace haibao \ design \ web \ view \ design;

Use haibao \ design \ web \ common \ design \ memento \ Originator;

Use haibao \ design \ web \ common \ design \ memento \ Caretaker;

Class Memento extends \ haibao \ design \ web \ view \ Base {

Protected function preRender (){

Header ("Content-type: text/html; charset = utf-8 ");

$ Originator = new Originator ();

$ Originator-> state = "ON ";

$ Originator-> show ();

$ Caretaker = new Caretaker ();

$ Caretaker-> setMemento ($ originator-> createMemento ());

$ Originator-> state = "OFF ";

$ Originator-> show ();

$ Originator-> setMemento ($ caretaker-> getMemento ());

$ Originator-> show ();

}

}

/**

* Manage roles

*/

Namespace haibao \ design \ web \ common \ design \ memento;

Class Caretaker {

Private $ memento;

Public function setMemento ($ memento ){

$ This-> memento = $ memento;

}

Public function getMemento (){

Return $ this-> memento;

}

}

/**

* Memorandum role

*/

Namespace haibao \ design \ web \ common \ design \ memento;

Class Mementor {

Private $ state;

Public function _ construct ($ state ){

$ This-> state = $ state;

}

Public function getState (){

Return $ this-> state;

}

}

/**

* Initiator role

*/

Namespace haibao \ design \ web \ common \ design \ memento;

Class Originator {

Public $ state;

Public function createMemento (){

Return new Mementor ($ this-> state );

}

Public function setMemento (Mementor $ memento ){

$ This-> state = $ memento-> getState ();

}

Public function show (){

Echo"
State = ". $ this-> state;

}

}

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.