15. StarCraft PHP design mode--strategy 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/4248199.html
==============================================================================


Star Open map battle, wait 5 seconds to enter the map, each player will have a base, a few farmers, and there will be the initial population supply. But these are different depending on the race.

problem to be solved: We need to make different initialization for the players based on race, and it's best to encapsulate these different processing methods.

idea: define the initialized interface and then make the initialization classes for different races.

Example policy mode (strategy):

In order to make the code not too long, the definition of a subset of classes is not written here, and if you want to debug, replace new with a string.

<?PHP//player's class    classPlayer {//belonging to the race         Public $race; //troops         Public $army; //Building         Public $building; //Population Supply         Public $supply; //constructor, set the race to belong to         Public function__construct ($race)        {            $this->race =$race; }    }    //Initialized Interface    InterfaceInitialplayer {//the troops that made the initialization         Public functionGivearmy ($player); //construction of an initialized building         Public functionGivebuilding ($player); //Initialize population supply         Public functionGivesupply ($player); }    //the initial algorithm of insect clan    classZerginitialImplementsInitialplayer {//the troops that made the initialization         Public functionGivearmy ($player)        {            //a overlord            $player->army[] =NewOverlord (); //Four Zerg Farmers             for($i= 0;$i<4;$i++)            {                $player->army[] =NewDrone (); }        }        //construction of an initialized building         Public functionGivebuilding ($player)        {            //a base            $player->building[] =NewHatchery (); }        //Initialize population supply         Public functionGivesupply ($player)        {            //the initial population supply of the Zerg is 9 .            $player->supply = 9; }    }    //the algorithm of the initialization of the human race    classTerraninitialImplementsInitialplayer {//the troops that made the initialization         Public functionGivearmy ($player)        {            //Four human-ethnic farmers             for($i= 0;$i<4;$i++)            {                $player->army[] =NewSVC (); }        }        //construction of an initialized building         Public functionGivebuilding ($player)        {            //a base            $player->building[] =NewHatchery (); }        //Initialize population supply         Public functionGivesupply ($player)        {            //the initial population supply of the human race is ten            $player->supply = 10; }    }    //Initialize the control class    classInitialcontroller {//constructor, the argument is the player's array         Public function__construct ($playerArray)        {            foreach($playerArray  as $player)            {                Switch($player-race) {                     Case' Zerg ':$initialController=Newzerginitial ();  Break;  Case' Terran ':$initialController=Newterraninitial ();  Break; }                $initialController->givearmy ($player); $initialController->givebuilding ($player); $initialController->givesupply ($player); }        }    }    //Suppose two Zerg, one clan    $playerArray=Array(        NewPlayer (' Zerg '),NewPlayer (' Zerg '),NewPlayer (' Terran '),    ); //Perform initialization work    $initialController=NewInitialcontroller ($playerArray);?>

Usage Summary: The strategy mode can encapsulate the algorithms in different situations and invoke them according to the specific situation.

Implementation Summary: need an interface, specify the algorithm specification, the user (such as initialization) as long as the corresponding algorithm can be called.

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

15. StarCraft PHP design mode--strategy 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.