Concept
Iterator mode (Iterator), also called cursor mode. Provides a way to sequentially access various elements in an aggregated object without exposing the object's internal representation.
You should consider using the iterator pattern when you need to access an aggregation object, and regardless of what these objects need to traverse. In addition, you may want to consider using the iterator pattern when there are multiple ways to traverse the aggregation. The iterator pattern provides a unified interface for traversing different aggregation structures, such as start, next, end, or current.
Applicable scenarios
Access the contents of an aggregated object without exposing its internal representation
Support for multiple traversal of aggregated objects
Provides a unified interface for traversing different aggregation structures
UML diagram
Role
Iterator (iterator): An iterator that defines an interface for accessing and traversing elements
Concreteiterator (Specific iterator): The specific iterator implements the iterator interface, which tracks the current position for the aggregation over duration
Aggregate (Aggregation): aggregation defines an interface for creating a corresponding iterator object
Concreteaggregate (Specific aggregation): A concrete aggregation implements an interface that creates an appropriate iterator that returns an appropriate instance of Concreteiterator
Code
The code is as follows:
PHP SPL has provided the iterator interface iterator and the container interface Iteatoraggragate, the source code is as follows:
/** * Interface to detect if a class is traversable using &foreach;. * @link http://php.net/manual/en/class.traversable.php */interface traversable {}/** * interface to create an external Ite Rator. * @link http://php.net/manual/en/class.iteratoraggregate.php */interface iteratoraggregate extends traversable {/** * Retrieve an external iterator * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return Tra Versable an instance of an object implementing <b>Iterator</b> or * <b>Traversable</b> * @ Since 5.0.0 */Public function getiterator ();} /** * Interface for external iterators or objects this can be iterated * themselves internally. * @link http://php.net/manual/en/class.iterator.php */interface iterator extends traversable {/** * Return the cur Rent element * @link http://php.net/manual/en/iterator.current.php * @return mixed Can return any type. * @since 5.0.0 * * Public Function CurrENT (); /** * Move forward to next element * @link http://php.net/manual/en/iterator.next.php * @return void any retur Ned value is ignored. * @since 5.0.0 */Public function next (); /** * Return The key of the current element * @link http://php.net/manual/en/iterator.key.php * @return Mixed Scalar on success, or null on failure. * @since 5.0.0 * * Public Function key (); /** * Checks If current position is valid * @link http://php.net/manual/en/iterator.valid.php * @return Boolea n The return value is casted to a Boolean and then evaluated. * Returns true on success or false on failure. * @since 5.0.0 */Public function valid (); /** * Rewind The Iterator to the first element * @link http://php.net/manual/en/iterator.rewind.php * @return Void any returned value is ignored. * @since 5.0.0 */Public Function rewind ();}
Here we directly implement the above two interfaces, see the following code:
<?phpheader (' Content-type:text/html;charset=utf-8 ');/** * Iterator mode *//** * Class concreteiterator specific iterator */class Concreteiterator implements iterator{Private $position = 0; Private $array = Array (); Public function __construct ($array) {$this->array = $array; $this->position = 0; } function Rewind () {$this->position = 0; } function current () {return $this->array[$this->position]; } function key () {return $this->position; } function Next () {+ + $this->position; } function valid () {return isset ($this->array[$this->position]); }}/** * Class myaggregate aggregation container */class Concreteaggregate implements iteratoraggregate{public $property; /** * Add Attribute * * @param $property */Public Function AddProperty ($property) {$this->propert Y[] = $property; } public Function Getiterator () {return new Concreteiterator ($this->property); }}/** * Class Client Client test */class client{public static function test () {//Create a container $concreteAggre Gate = new Concreteaggregate (); Add Property $concreteAggregate->addproperty (' attribute 1 '); Add Property $concreteAggregate->addproperty (' attribute 2 '); Create an iterator to the container $iterator = $concreteAggregate->getiterator (); Traverse while ($iterator->valid ()) {$key = $iterator->key (); $value = $iterator->current (); Echo ' key: '. $key. ' Value: '. $value. '
Operation Result:
Key: 0 Value: Attribute 1
Key: 1 Value: Attribute 2