Memorandum mode and php implementation

Source: Internet
Author: User
Memorandum mode and php implementation memorandum mode:
It is also called the snapshot mode or Token mode. it captures the internal state of an object without interrupting the closed state, and saves the state outside the object. In this way, the object can be restored to the previously saved state.

Role:
1. creator: Creates a memorandum to record the internal status of the current moment and restores the internal status of the memorandum. The initiator can determine the internal states of the memorandum as needed.
2. Memorandum: stores the internal status of the initiator object and prevents other objects other than the initiator from accessing the memorandum. The memorandum has two interfaces: the manager can only see the narrow interface of the memorandum, and he can only pass the memorandum to other objects. The initiator can see the wide interface of the memo, allowing it to access all data returned to the previous status.
3. administrator: The administrator is responsible for accessing the memorandum and cannot access or operate on the content.

UML class diagram:

   

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.

Code implementation:

 Id = $ id; $ this-> name = $ name; $ this-> liveLevel = $ liveLevel ;}} // memorandum manager class Originator {public static $ mementos = array (); private static $ instance = null; // The Singleton mode ensures that only one manager private function _ construct () {} // returns the single-instance object static function getInstance () {if (! (Self: $ instance instanceOf slef) {self: $ instance = new self ();} return self: $ instance ;} // Save the memo function setMemento ($ id, Memento $ memento) {self: $ mementos [$ id] = $ memento;} // Obtain the memo function getMemento ($ id) {return self: $ mementos [$ id] ;}// creator, Player, class Player {private static $ I = 0; // static variable accumulation is used to assign a public $ id to $ id; // Each object is unique and used to save the state memo to the manager private $ name; // name private $ liveLevel; // life value function _ construct ($ name) {$ this-> name = $ name; $ this-> id = self ::$ I; self :: $ I ++;} // initialize function init () {$ this-> liveLevel = 100;} // reduce the life value by 10 function damage () {$ this-> liveLevel-= 10;} // display the existing status function displayState () {echo "last name :". $ this-> name."
"; Echo" Lifecycle: ". $ this-> liveLevel ."
";}// Save the status to a memo, which will be placed in the manager function saveState () {$ originator = Originator: getInstance (); $ originator-> setMemento ($ this-> id, new Memento ($ this-> id, $ this-> name, $ this-> liveLevel ));} // restore the memo function getState () {$ originator = Originator: getInstance (); $ memento = $ originator-> getMemento ($ this-> id ); $ this-> id = $ memento-> id; $ this-> name = $ memento-> name; $ this-> liveLevel = $ memento-> liveLevel ;}} // Test // Create and initialize a role and display the status $ player = new Player ("Zhangsan"); $ player-> init (); $ player-> displayState (); // lifecycle 100 // archive before starting the game $ player-> saveState (); // start the game, reduce the number of damage Records received, and display the status $ player-> damage (); $ player-> damage (); $ player-> displayState (); // life value 80 // restore to the original state, display status $ player-> getState (); $ player-> displayState (); // The lifecycle is 100?>

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.