<?php /** **迭代器设计模式:通过某种对象集合或对象数组封装迭代的方法 **作用:隐藏内部数据结构 **实现:通过一个类,可以访问到内部所有变量,却不必知道内部细节 ***/ interface Interation{ public function first(); public function next(); public function current(); public function isDone(); } class Interator implements Interation{ private $iterator = array(); private $_position = 0; public function __construct($_iterator,$position=‘‘){ $this->iterator = $_iterator; $this->_position=$position; } public function first(){ return $this->iterator[0]; } public function next(){ return $this->iterator[($this->_postion) + 1]; } public function current(){ return $this->iterator[$this->_position]; } public function isDone(){ if($this->_postion == count($this->iterator)){ return false; }else{ return true; } } } class Client{ public static function main(){ $iter = new Interator(array(11,3,5,34,2,89)); echo ‘1‘.$iter->first(); echo ‘2‘.$iter->next(); echo ‘3‘.$iter->current(); echo ‘4‘.$iter->isDone(); } } Client::main();
本文出自 “王尼美的成人之路” 博客,请务必保留此出处http://8335914.blog.51cto.com/8325914/1611199
原文地址:http://8335914.blog.51cto.com/8325914/1611199