您好,欢迎访问一九零五行业门户网

PHP设计模式之迭代器模式

概念
迭代器模式(iterator),又叫做游标(cursor)模式。提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。
当你需要访问一个聚合对象,而且不管这些对象是什么都需要遍历的时候,就应该考虑使用迭代器模式。另外,当需要对聚集有多种方式遍历时,可以考虑去使用迭代器模式。迭代器模式为遍历不同的聚集结构提供如开始、下一个、是否结束、当前哪一项等统一的接口。
适用场景
访问一个聚合对象的内容而无需暴露它的内部表示
支持对聚合对象的多种遍历
为遍历不同的聚合结构提供一个统一的接口
uml图
角色
iterator(迭代器):迭代器定义访问和遍历元素的接口
concreteiterator(具体迭代器):具体迭代器实现迭代器接口,对该聚合遍历时跟踪当前位置
aggregate (聚合): 聚合定义创建相应迭代器对象的接口
concreteaggregate (具体聚合):具体聚合实现创建相应迭代器的接口,该操作返回concreteiterator的一个适当的实例
代码
代码如下:
php spl中已经提供了迭代器接口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 值: 属性1
键: 1 值: 属性2
其它类似信息

推荐信息