Home  >  Article  >  Backend Development  >  PHP object-oriented advanced design patterns: iterator pattern usage examples

PHP object-oriented advanced design patterns: iterator pattern usage examples

巴扎黑
巴扎黑Original
2017-05-23 13:45:421536browse

What is the iterator pattern?

The Iterator design pattern helps construct objects that provide a single standard interface for looping or iterating over any type of countable data.

Iterator Pattern Problems and Solutions:

The Iterator design pattern helps style objects to handle collections of data or other objects. When creating a class based on the Iterator design pattern, we also create a set of interfaces to provide a unified way to manage these collections.

Sometimes, the data set seems very simple. Programmers may not anticipate changes, so they choose not to modify their code and not create iterators. This situation often occurs when calling the database. At this point, the programmer creates a MySQL query and then executes a simple fetch array command. However, keeping such a procedural approach in the code is not the best solution.

Iterator objects should be created when processing MySQL result sets. A simpler approach is to provide a MySQL query to the class constructor and then loop through the result set by calling the public methods of the iterator object. More complex iterator examples may also have additional parameters to be sent to the iterator, and depending on these conditions, it is possible to execute different sets of MySQL queries. But anyway, the outer code flow just handles the same annoying public method to get the next item in the collection.

When dealing with countable 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 using the iterator design pattern.

PHP object-oriented advanced design patterns: iterator pattern usage examples

The following is an explanation of the above picture:

1.MyObject is a base object that can be collected into a countable collection. MyObject has a private string named name, which is used to represent the uniqueness of a specific object. The public method getName() provides an interface for retrieving the object name through the private name.

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

3.MyObjectCollectionIterator provides an interface for iterating the objects stored in MyObjectCollection. This class has two public methods. Among them, the hasNext() method allows the calling program to know whether there are other items in the MyObjectCollection collection of MyObjects: the getNext() method will return the next MyObject object in the array in MyObjectCollection.

Usage Example

This example shows the calling sequence of iterator methods when using foreach.

<?php
class 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";
}
?>

The above is the detailed content of PHP object-oriented advanced design patterns: iterator pattern usage examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn