11. The PHP design mode of StarCraft--Memo mode

Source: Internet
Author: User

Preface
==============================================================================
This PHP design mode album from the blog (jymoz.com), now has no access, this series of articles I have been looking for a long time to find the complete, thanks to the author Jymoz's Hard Pay Oh!

This address:http://www.cnblogs.com/davidhhuan/p/4248190.html
==============================================================================


When we play Star Quest or stand-alone with the computer, sometimes we suddenly leave the game, or in front of the troops, need to save the game.
So what do we do to save the current information? And at any time, you can restore the saved game?

problem to be solved: Save All the information of the game and restore it completely if it is restored.

idea: Create a class that specifically preserves information so he can handle these things like a memo.

For the sake of simplicity, we use this information to restore a player to demonstrate.

Memo (Memento) mode example:

<?PHP//Memo Class    classMemento {//Crystal Mine         Public $ore; //Gas Mine         Public $gas; //all of the player's Army objects         Public $troop; //all building objects of the player         Public $building; //constructor, parameter is the object of the player to be saved, the type of the force argument here is the player class         Public function__construct (Player$player)        {            //Save this player's Crystal mine            $this->ore =$player-ore; //Save this player's gas mine            $this->gas =$player-Gas ; //Save all of the player's Army objects            $this->troop =$player-Troop; //Save all building objects for this player            $this->building =$player-Building; }    }    //player's class    classPlayer {//Crystal Mine         Public $ore; //Gas Mine         Public $gas; //all of the player's Army objects         Public $troop; //all building objects of the player         Public $building; //get this player's memo object         Public functionGetmemento () {return NewMemento ($this); }        //Use this player's memo object to restore this player, here the mandatory parameter type is memento class         Public functionRestore (Memento$m)        {            //Crystal Mine            $this->ore =$m-ore; //Gas Mine            $this->gas =$m-Gas ; //all of the player's Army objects            $this->troop =$m-Troop; //all building objects of the player            $this->building =$m-Building; }    }    //make a player    $p 1=NewPlayer (); //Assuming he's now picking up 100 crystal mines.    $p 1->ore = 100; //Let's save the game and continue playing the game.    $m=$p 1-Getmemento (); //Assuming he's now picking up 200 crystal mines.    $p 1->ore = 200; //We are now loading the original saved game    $p 1->restore ($m); //output Crystal ore, you can see that it has become the original preservation of the state    Echo $p 1-ore;?>

Usage Summary: The memo mode allows us to save information for a moment, and then restore the information needed, just as the game is saved and loaded into the archive.

Implementation Summary: a memo class is required to hold the information, the saved class needs to implement the method of generating the memo object, and the method of invoking the memo object to restore its state.

Related articles:

1. StarCraft PHP Object-oriented (i)

2. StarCraft PHP Object-oriented (ii)

3. StarCraft PHP design mode-Simple Factory mode

4. StarCraft PHP Design mode-factory method mode

5. StarCraft PHP design mode-Abstract Factory mode

6. StarCraft PHP design mode-builder mode

7. The PHP design mode of StarCraft--The mediator mode

8. StarCraft PHP design mode--Enjoy meta mode

9. StarCraft PHP Design mode--proxy mode

10. StarCraft PHP design mode-prototype mode

11. The PHP design mode of StarCraft--Memo mode

12. StarCraft PHP design mode-template mode

13. StarCraft PHP design mode-positive mode

14. StarCraft PHP design mode-state mode

15. StarCraft PHP design mode--strategy mode

16. StarCraft PHP Design mode--combination mode

17. StarCraft PHP Design mode--responsibility chain mode

18. StarCraft PHP design mode-Observer mode

19. The PHP design mode of StarCraft--iterator mode

? 20. StarCraft PHP design mode-Adapter mode

11. The PHP design mode of StarCraft--Memo mode

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.