PHP Object-oriented Advanced design pattern: Iterator pattern usage examples

Source: Internet
Author: User
What is an iterator pattern?

Iterator design patterns can help construct specific objects that can provide a single standard interface loop or iterate over any type of data that can be counted.

Iterator pattern problems and solutions:

The iterator design pattern helps style objects work with collections of data or other objects. When you create a class that is based on an iterator design pattern, we also create a set of interfaces to provide a uniform way to manage these collections.

In some cases, datasets seem to be very simple. Programmers will not be able to anticipate what is going to change, so they choose not to modify the code and not create iterators. This is often the case when the database is called. At this point, the programmer creates the MySQL query and then executes a simple fetch array command. However, keeping such procedural methods in your code is not the best solution.

You should create an iterator object when working with a MySQL result set. It is relatively straightforward to provide a MySQL query for the class constructor and then iterate through the loop result set by invoking the public method of the iterator object. A more complex iterator example might also have additional parameters to send to the iterator, which, depending on these conditions, might execute different sets of MySQL queries. However, the external code flow only gets the next item in the collection by handling the same annoying public method.

When dealing with the data that needs to be traversed, the best solution is to create an object based on the iterator pattern.

Uml

This diagram details a class design that uses an iterator design pattern.

Here is a description of the pair:

1.MyObject is a base object that can be collected into a set of technical numbers. MyObject has a private string named name that is used to represent the uniqueness of a particular object. Public Method GetName () provides an interface to retrieve the name of an object through the private name name.

2.MyObjectCollection represents a class that manages a collection of MyObject objects. The myobjects array holds the collection of these objects. The logic provided by Getmyobjects () is used to create collections and store objects in the myobjects array.

The 3.MyObjectCollectionIterator provides an interface for iterating over the objects stored in the myobjectcollection. This class has two public methods. where the Hasnext () method causes the caller to know if there are other items in the Myobjects myobjectcollection collection: The GetNext () method returns the next Myobjectcollection object in the array in MyObject.

Working with instances

This example shows the sequence of calls to the Iterator method when using foreach.

<?phpclass Myiterator implements Iterator {private $position = 0;      Private $array = Array ("Firstelement", "Secondelement", "lastelement",);    Public Function __construct () {$this->position = 0;        } function Rewind () {var_dump (__method__);    $this->position = 0;        } function current () {var_dump (__method__);    return $this->array[$this->position];        } function key () {var_dump (__method__);    return $this->position;        } function Next () {var_dump (__method__);    + + $this->position;        } function valid () {var_dump (__method__);    return Isset ($this->array[$this->position]);    }} $it = new Myiterator;foreach ($it as $key = = $value) {var_dump ($key, $value); echo "\ n";}? 

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.