PHP design Mode-Memo mode

Source: Internet
Author: User

Statement: This series of blog reference "Big Talk design mode", author Geoscience.


Memo Mode also called snapshot mode or Token mode that captures an object without breaking the enclosing condition . Internal State , and in that Save this state outside of the object . The object can then be restored to its previously saved state.


UML Class Diagrams:


role:

1. Initiator (gamerole): responsible for creating a memo that records the internal state of the current moment, And you can use the memo to restore the internal state. The initiator can decide what internal state the memo stores itself as needed.

2. Memo (rolestatesavebox): responsible for storing the internal state of the initiator object , You can also prevent objects other than the initiator from accessing the memo. The memo has two interfaces: the manager can only see the narrow interface of the memo, and he can only pass the memo to other objects. The initiator can see the wide interface of the memo, allowing it to access all the data needed to return to the previous state.

3. Manager (gamerolestatemanager): responsible for accessing memos, cannot access or operate the content.


Core code:

<?php/** * Created by Phpstorm.     * User:jang * DATE:2015/6/10 * time:9:49 *///Game character class gamerole{#region game character State properties (vitality, attack, defense) public $liveLevel;    Public $attackLevel;    Public $defenseLevel; #endregion//Save state Public Function SaveState () {return (new Rolestatememento ($this->livelevel, $this-&gt    ; Attacklevel, $this->defenselevel)); }//Recovery state Public Function recoverystate (Rolestatememento $_memento) {$this->livelevel = $_memento->l        Ivelevel;        $this->attacklevel = $_memento->attacklevel;    $this->defenselevel = $_memento->defenselevel; }//------------other properties and operations--------------//Get initial state public Function getinitstate () {$this->defenselevel        = 100;        $this->attacklevel = 100;    $this->livelevel = 100;        }//Status display public function Statedisplay () {echo "Role status:<br/>";        echo "Vitality: {$this->livelevel}<br/>"; echo "Damage: {$this->attackleVel}<br/> ";    echo "defensive power: {$this->defenselevel}<br/>";        }//Attacked Public function Beenattack () {$this->livelevel-= 9.5;            if ($this->livelevel<=0) {$this->livelevel = 0; echo "Well, the character is dead!"            <br/> ";            echo "Game over!<br/>";        Return        } $this->attacklevel-= 1.1;        if ($this->attacklevel<=0) {$this->attacklevel = 0;        } $this->defenselevel-= 0.5;        if ($this->defenselevel<=0) {$this->defenselevel = 0;    }}}//role state storage box class rolestatememento{public $liveLevel;    Public $attackLevel;    Public $defenseLevel;        Public Function Rolestatememento ($_LL,$_AL,$_DL) {$this->livelevel=$_ll;        $this->attacklevel=$_al;    $this->defenselevel=$_dl; }}//Game role state manager class rolestatemanager{public $memento;}

Test code:
<?php/** * Created by Phpstorm. * User:jang * DATE:2015/6/10 * time:10:02 */header ("Content-type:text/html;charset=utf-8");//----------------------- --Memo mode------------------------require_once "./memento/memento.php";//Before the war $ufo = new Gamerole (); $ufo Getinitstate (); echo "<span style= ' color: #ff0000 ' >----------------before the War-----------------</span><br/ > "; $ufo->statedisplay ();//Save Progress $roleman = new Rolestatemanager (); $roleMan->memento = $ufo->savestate (); echo "<span style= ' color: #ff0000 ' >-----------------</span><br/> in Battle----------------; $num =1;//    War BOSS5 round for ($i = 0; $i <13; $i + +) {echo "-------------{$num} round-------------<br/>";    $ufo->beenattack ();    $ufo->statedisplay ();    $num + +;    Character dead if ($ufo->livelevel<=0) {break; }}echo "<span style= ' color: #ff0000 ' >----------------recovery status-----------------</span><br/>"; Pre-restore State $ufo->recoverystate ($roleMan->memento); $ufo->statedisplaY (); 


Advantages:

1 , sometimes the internal information of some initiator objects must be stored outside the initiator object, but must be read by the initiator object itself , the use of Memo mode allows the complex initiator's internal information to be shielded from other objects so that the encapsulated boundaries can be properly maintained.

2 , this model simplifies the initiation of human beings. The initiator no longer needs to manage and save a version of its internal state, and the client can manage the versions of those states they need.


Disadvantages:

1 , if the status of the initiator role needs to be stored completely in the memo object, the memo object will be expensive on resource consumption.

2 , when the responsible role stores a memo, the person in charge may not know how much storage space the state consumes, and thus cannot alert the user whether an operation is expensive.



Welcome to follow my video course, the address is as follows, thank you.


PHP Object-oriented design pattern

PHP design Mode-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.