PHP design Mode-Memo mode

Source: Internet
Author: User

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


Memo Mode also called snapshot mode or Token mode, without breaking the closed premises. captures the internal state of an object and saves the 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 is able to decide which internal state the memo stores itself, depending on the need.

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

3. Manager (gamerolestatemanager): responsible for accessing the memo, not the content of the interview or operation.


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}&Lt;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 (); 


Strengths:

1 , sometimes the internal information of some initiator objects must be kept outside the initiator object. However, it must be read by the initiator object itself . In this case, 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 has to manage and save a version number of its internal state, and the client is able to manage the version number of the states they need.


Disadvantages:

1 , assuming that the status of the initiator role needs to be stored completely in the memo object, the memo object can be very expensive on resource consumption.

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



You are 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.