aggregate = $aggregate; $this->aggregateCount = $this->aggregate->getCounts(); } public function first(){ $this->index = 0; } public function next(){ if($this->index < $this->aggregateCount){ $this->index = $this->index + 1; } } public function isDone(){ if($this->index == $this->aggregateCount){ return true; } return false; } public function currentItem(){ return $this->aggregate->getItem($this->index); } public function getAggregateCount(){ return $this->aggregateCount; }} /** * 聚合对象的抽象类,定义了创建相应迭代器对象的接口,每一个实现该聚合对象抽象类的对象都要实现这个抽象方法 */abstract class Aggregate{ public abstract function createIterator();} /** * 具体的聚合对象,实现创建相应迭代器对象的功能 */class ConcreteAggregate extends Aggregate{ //聚合对象的具体内容 private $arrayAgg = null; /** * 构造函数:传入聚合对象具体的内容,在这个例子中是数组 * @param $arrayAgg 聚合对象的具体内容 */ public function __construct($arrayAgg){ $this->arrayAgg = $arrayAgg; } public function createIterator(){ //实现创建Iterator的工厂方法 return new ConcreteIterator($this); } public function getItem($index){ if($index < sizeof($this->arrayAgg)){ return $this->arrayAgg[$index]; } } public function getCounts(){ return sizeof($this->arrayAgg); }} /** * 看看具体的使用方法 */$aggregate = new ConcreteAggregate(array('张三','李四','王五'));$iterator = $aggregate->createIterator(); $iterator->first();while(!$iterator->isDone()){ $obj = $iterator->currentItem(); echo "The obj == ".$obj.""; $iterator->next();}?>
UML