概念
迭代器模式(Iterator),又叫做遊標(Cursor)模式。提供一種方法順序存取一個聚合物件中的各種元素,而又不暴露該物件的內部表示。
當你需要存取一個聚合對象,而且不管這些對像是什麼都需要遍歷的時候,就應該考慮使用迭代器模式。另外,當需要對聚集有多種方式遍歷時,可以考慮去使用迭代器模式。迭代器模式為遍歷不同的聚集結構提供如開始、下一個、是否結束、目前哪一項等統一的介面。
適用場景
訪問一個聚合對象的內容而無需暴露它的內部表示
支援對聚合對象的多種遍歷
為遍歷不同的聚合結構提供一個統一的接口
UML1
角色Iterator(迭代器):迭代器定義訪問和遍歷元素的接口ConcreteIterator(具體迭代器):具體迭代器實現迭代器接口,對該聚合歷時跟踪當前位置Aggregate (聚合) : 聚合定義建立對應迭代器物件的介面ConcreteAggregate (具體聚合):具體聚合實作建立對應迭代器的接口,該動作傳回ConcreteIterator的適當的實例程式碼程式碼如下:PHP中
PHP已經提供了迭代器接口Iterator和容器接口IteatorAggragate,其源碼如下:
/** * 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 Iterator. * @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 Traversable 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 that can be iterated * themselves internally. * @link http://php.net/manual/en/class.iterator.php */ interface Iterator extends Traversable { /** * Return the current 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 returned 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 boolean The return value will be casted to 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(); }
這裡我們直接實現上面兩個接口,請看下面代碼:
<?php header('Content-type:text/html;charset=utf-8'); /** * 迭代器模式 */ /** * Class ConcreteIterator 具体的迭代器 */ 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 聚合容器 */ class ConcreteAggregate implements IteratorAggregate { public $property; /** * 添加属性 * * @param $property */ public function addProperty($property) { $this->property[] = $property; } public function getIterator() { return new ConcreteIterator($this->property); } } /** * Class Client 客户端测试 */ class Client { public static function test() { //创建一个容器 $concreteAggregate = new ConcreteAggregate(); // 添加属性 $concreteAggregate->addProperty('属性1'); // 添加属性 $concreteAggregate->addProperty('属性2'); //给容器创建迭代器 $iterator = $concreteAggregate->getIterator(); //遍历 while($iterator->valid()) { $key = $iterator->key(); $value = $iterator->current(); echo '键: '.$key.' 值: '.$value.'<hr>'; $iterator->next(); } } } Client:: test();
運行結果:
鍵: 0 值