19. The PHP design mode of StarCraft--iterator 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/4248206.html
==============================================================================


Interstellar missions usually have this setting: When a computer farmer does not mining, if the battle starts, or if the player makes the first soldier, the computer farmer begins mining.
We would naturally think of putting the computer's farmer into an array, and then, once the player is created, or fighting, loop the array and let the farmers inside.
But the problem comes out, because each task's setting will be different, we always hope that the development of the task is more convenient, and easy to modify (once the bug is found).
Moreover, some tasks are not farmers mining, but computers attacking players.
So too many fixed details (stored in arrays) and dependency details (array loops) will make the code much more relevant.

the problem to be solved: to abstract the transaction of the loop processing.

idea: The key is the cycle of farmers, using arrays to deal with just one way, we consider abstract arrays instead of concrete arrays.

Example of an iterator (Iterator) pattern:

<?PHP//aggregation interface, meaning that all computer farmers are gathered in this class    InterfaceIaggregate {//For a specific aggregation class to implement, get the method of using the iterator         Public functionCreateiterator (); }    //the specific aggregation class    classConcreteaggregateImplementsIaggregate {//Store The farmer's array, note that you can use the array to deal with, read all the code will know         Public $workers; //adding elements to the method, where the element is the farmer         Public functionAddElement ($element)        {            $this->workers[] =$element; }        //methods to get elements         Public functionGetAt ($index)        {            return $this->workers[$index]; }        //methods to get the number of elements         Public functionGetLength () {return Count($this-workers); }        //ways to get iterators         Public functionCreateiterator () {return NewConcreteiterator ($this); }    }    //iterator interface, note that PHP5 has a built-in interface called iterator, so here we change to Iiterator    InterfaceIiterator {//whether the element loop is complete         Public functionHasnext (); //returns the next element and adds the pointer 1         Public function Next(); }    //Specific iterator Classes    classConcreteiteratorImplementsIiterator {//the collection to iterate on         Public $collection; //Pointers         Public $index; //constructor to determine the collection of iterations and place the pointer 0         Public function__construct ($collection)        {            $this->collection =$collection; $this->index = 0; }        //whether the element loop is complete         Public functionHasnext () {if($this->index <$this->collection->GetLength ()) {                return true; }            Else            {                return false; }        }        //returns the next element and adds the pointer 1         Public function Next()        {            $element=$this->collection->getat ($this-index); $this->index++; return $element; }    }    //Initialize the computer to the farmer's aggregation object    $farmerAggregate=Newconcreteaggregate (); //Add farmers, here is a simple string representation    $farmerAggregate->addelement (' SVC1 '); $farmerAggregate->addelement (' SVC2 '); //Get iterators    $iterator=$farmerAggregate-Createiterator (); //cycle the farmers ' gathering objects     while($iterator-Hasnext ()) {        //get the next farmer        $element=$iterator-Next(); //our simple output        Echo $element; }?>

Usage Summary: The iterator pattern establishes an array-like form, which you can see from the code above that if you want to modify the processing of a loop, or modify a collection that is looped, you do not have to modify other related code.

Implementation Summary: A class that manages aggregation is required, such as the concreteaggregate above. An iterator class is also required, such as the concreteiterator above. It then abstracts all operations, such as adding elements, getting the next element, pointers, and so on, so that the other code simply uses methods such as GetLength () rather than the detailed count () function, so that even without the array to store the farmer, There is no need to change the code outside of the aggregation class.

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

19. The PHP design mode of StarCraft--iterator 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.