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;
}
}