14. StarCraft PHP design mode-state 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/4248198.html
==============================================================================


Some star classes will have more than one state, such as tanks can be rack up, the machine gun can play doping, and even some passive, such as by the Zerg queen sprayed green liquid, the enemy's action slowed.
If, according to the general idea, every time we operate on a creep, such as a tank, we have to use if to determine his state, so there will be a lot of if,else or Swith in the code.
But we can see that what we need is his behavior in a certain state, and if we encapsulate these behaviors in a state, we can reduce a lot of judgment.

problem to be solved: encapsulate the state of the tank and let the state control its behavior.

idea: the state as a property, the type of soldiers itself only control the change of state, the specific behavior by the State class definition.

Example status (state) mode:

<?PHP//interface of the tank state    InterfaceTankstate {//attack methods of tanks         Public functionattack (); }    //Tank General Status    classTankstate_tankImplementsTankstate {//attack methods of tanks         Public functionattack () {//Here's a simple output of the current state            Echo"Normal state"; }    }    //the state of a tank rack .    classTankstate_siegeImplementsTankstate {//attack methods of tanks         Public functionattack () {//Here's a simple output of the current state            Echo"Stand up."; }    }    //Tank Class    classTank {//Status         Public $state; //attack methods of tanks         Public function__construct () {//the newly created tank is, of course, a normal state.            $this->state =NewTankstate_tank (); }        //The method of setting the state, assuming that the parameter is the keyboard that the player clicked on         Public functionSetState ($key)        {            //If you press the S            if($key= ' s ')            {                $this->state =NewTankstate_siege (); }            //If you press the T            ElseIf($key= ' t ')            {                $this->state =NewTankstate_tank (); }        }        //attack methods of tanks         Public functionattack () {//by the current state itself to handle the attack            $this->state->attack (); }    }    //build a new tank .    $tank=NewTank (); //assuming there's an enemy passing by, the tank attacked in normal mode.    $tank-attack (); //Rack up Tanks    $tank->setstate (' s ')); //Tank attack Again, this time it's a pattern.    $tank-attack ();?>

Usage Summary: state mode can encapsulate the behavior and attributes associated with the state, and in addition to switching states, there is no need to make a large number of judgments about the current state, as long as the current state method is called.

Implementation Summary: use an interface to standardize the way the State class needs to be implemented, such as the above tankstate specifies attack (). Encapsulate each state into a class, placing different methods in different states into their state classes, such as the above attack method, and all state execution interfaces. The original transaction class, such as the above tank class, is only responsible for the state switch, once the call of a certain method, as long as the current state to be able to.

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

14. StarCraft PHP design mode-state 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.