PHP design pattern-Iterator pattern (Iterator)

Source: Internet
Author: User
The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). PHP has built the Iterator interface since PHP5.3.

The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). Starting from PHP 5.3, PHP has built in the Iterator interface, implementing overlap for us.

The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). Starting from PHP 5.3, PHP has built the Iterator interface, which facilitates implementation of the Iterator mode:


// The Iterator interface provided by PHP5. The Traversable interface is only used to identify whether an object can be iterated through foreach. It does not contain any Iterator extends Traversable {/* Methods */abstract public mixed current (void) abstract public scalar key (void) abstract public void next (void) abstract public void rewind (void) abstract public boolean valid (void )}

Maybe you may find it strange (or redundant). For PHP, arrays themselves support iteration. Why do we need to encapsulate more layers through the iterator mode? This is the core of pattern definition: The visitor does not have to worry about the internal implementation of aggregate objects (such as the number of elements, KEY, and specific structure ); this means that the internal implementation of the aggregate object is not exposed to the visitor (for example, the object cannot be modified by the visitor ).

Implement our own iterator:

Class RecordIterator implements Iterator {private $ position = 0; // Note: The property of the object to be iterated is private $ records = array (); public function _ construct (Array $ records) {$ this-> position = 0; $ this-> records = $ records;} function rewind () {$ this-> position = 0;} function current () {return $ this-> records [$ this-> position];} function key () {return $ this-> position;} function next () {++ $ this-> position;} function valid () {return isset ($ this-> records [$ this-> position]);} // if we read the following data from MongoDB $ data = array (0 => array ('field' => 'value '), 1 => array ('field' => 'value'), 2 => array ('field' => 'value '), 3 => array ('field' => 'value'),); // use our own iterator $ records = new RecordIterator ($ data ); while ($ records-> valid () {print_r ($ records-> current (); $ records-> next () ;}$ records-> rewind ();

In the GoF-defined iterator mode, there are two participants: the container and the specific container, responsible for initializing and returning the specific iterator.


// Container interface Aggregate {public getIterator () ;}// the specific container class RecordList implements Aggregate {private $ iterator; public function _ construct ($ data) {$ this-> iterator = new RecordIterator ($ data);} public function getIterator () {return $ this-> iterator ;}} // use // if we obtain the following data from MongoDB $ data = array (0 => array ('field' => 'value '), 1 => array ('field' => 'value'), 2 => array ('field' => 'value '), 3 => array ('field' => 'value'),); $ recordList = new RecordList ($ data); $ iterator = $ recordList-> getIterator (); while ($ iterator-> valid () {print_r ($ iterator-> current (); $ iterator-> next ();}

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.