PHP design pattern: Iterator pattern (3)

Source: Internet
Author: User
Tags spl
Note that the Iterator is better abstract than the object set, because we can make InfiniteIterators, NoRewindIterators, and so on without having to be consistent with the regular array. Therefore, Iterator lacks functions such as the count () function. You can find the complete SPL iterator list in the official PHP Manual. Thanks to strong support for PHP, S

Note that the Iterator is better abstract than the object set, because we can make InfiniteIterators, NoRewindIterators, and so on without having to be consistent with the regular array. Therefore, Iterator lacks functions such as the count () function.
You can find the complete SPL iterator list in the official PHP Manual. Thanks to the strong support for PHP, most of the work using the Iterator mode is included in the standard implementation. the following code example uses the functions of the standard Iterator and RecursiveIterators.

___ FCKpd ___ 0/*** Usually IteratorAggregate is the interface to implement. * It has only one method, which must return an Iterator * already defined as another class (e.g. arrayIterator) * Iterator gives a finer control over the algorithm, * because all the hook points of Iterator contract * are available for implementation. */class NumbersSet implements IteratorAggregate {

The main character of the "PHP design patterns" series today is the Iterator pattern, which provides an abstraction in a common process: iteration on a set of objects (or scalar) that are not part of an object graph. Iterations have several different execution methods: Iteration over an array attribute, set object, array, or even a query result set.


In the object world, the iterator mode needs to maintain functions similar to Arrays. it is regarded as a non-invasive facet. the Client class is often separated from the real object implementation, which refers to the iterator interface. Whenever possible, we can send a reference to the iterator to replace the specific or abstract classes that may change in the future.
Iterator mode
Participants:
◆ Client: the method that references the iterator mode executes a loop on a group of values or objects.
◆ Iterator: Abstraction in the iteration process, including next (), isFinished (), current (), and other methods.
◆ ConcreteIterators: implements iteration on a specific object set, such as an array, tree, combination, and set.
Through the Traversable interface, PHP originally supports the Iterator mode, which is extended by Iterator and IteratorAggregate. These two subinterfaces not only define a set of standard methods, each Traversable object can be passed to foreach () in an intact manner. foreach is the main client of the Iterator, Iterator implementation is the real Iterator, and IteratorAggregate is a Traversable object with other responsibilities, it returns an Iterator through the getIterator () method.

The standard PHP library is the only universal object-oriented library bound to PHP and defines additional interfaces and public classes. OuterIterator implements the decoration of an Iterator. CachingIterator and LimitIterator are two examples of this interface.
RecursiveIterator is an extension implemented by the Iterator interface for the tree structure. it defines a set of additional methods to check whether the sub-objects of the current element in an iteration exist. RecursiveArrayIterator and RecursiveDirectoryIterator are implementation examples of this interface. these types of Iterators can be used as they are, or a RecursiveIteratorIterator Bridge is connected to a common iterator contract. This OuterIterator implements depth-first or breadth-first traversal based on the construction parameters.
When using RecursiveIteratorIterator, you can pass the recursiveiteratator to foreach. please refer to the code examples below to learn about the different usage of RecursiveIterators and their superset Iterator. Finally, SeekableIterators adds a seek () method to the contract, which can be used to move the internal state of the Iterator to a specific iteration point.

 

 

 

Related Article

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.