设计模式入门-迭代器模式(php版)
来源:互联网 发布:淘宝历史版本下载5.80 编辑:程序博客网 时间:2024/04/30 12:50
在深入研究这个设计模式之前,我们先来看一道面试题,来自鸟哥的博客,
题目是这样的:
使对象可以像数组一样进行foreach循环,要求属性必须是私有。
不使用迭代器模式很难实现,先看实现的代码:
sample.php
<?phpclass Sample implements Iterator{private $_arr;public function __construct(Array $arr){$this->_arr = $arr;}public function current(){ return current($this->_arr);}public function next(){ return next($this->_arr);}public function key(){ return key($this->_arr);}public function valid(){ return $this->current() !== false;}public function rewind(){ reset($this->_arr);}}
index.php
<?phprequire 'Sample.php';$arr = new Sample(['max', 'ben', 'will']); foreach ($arr as $k=>$v){ echo $k."-".$v."<br />";}
其中Iterator接口来自php的spl类库,在写完设计模式的相关文章之后,将会进一步研究这个类库。
另外在网上找到了一段yii框架中关于迭代器模式的实现代码:
class CMapIterator implements Iterator {/*** @var array the data to be iterated through*/ private $_d;/*** @var array list of keys in the map*/ private $_keys;/*** @var mixed current key*/ private $_key; /*** Constructor.* @param array the data to be iterated through*/ public function __construct(&$data) { $this->_d=&$data; $this->_keys=array_keys($data); } /*** Rewinds internal array pointer.* This method is required by the interface Iterator.*/ public function rewind() { $this->_key=reset($this->_keys); } /*** Returns the key of the current array element.* This method is required by the interface Iterator.* @return mixed the key of the current array element*/ public function key() { return $this->_key; } /*** Returns the current array element.* This method is required by the interface Iterator.* @return mixed the current array element*/ public function current() { return $this->_d[$this->_key]; } /*** Moves the internal pointer to the next array element.* This method is required by the interface Iterator.*/ public function next() { $this->_key=next($this->_keys); } /*** Returns whether there is an element at current position.* This method is required by the interface Iterator.* @return boolean*/ public function valid() { return $this->_key!==false; }} $data = array('s1' => 11, 's2' => 22, 's3' => 33);$it = new CMapIterator($data);foreach ($it as $row) { echo $row, '<br />';}
关于迭代器设计模式官方的定义是:使用迭代器模式来提供对聚合对象的统一存取,即提供一个外部的迭代器来对聚合对象进行访问和遍历 , 而又不需暴露该对象的内部结构。又叫做游标(Cursor)模式。
好吧,我不是很能理解。为什么明明数组已经可以用foreach来遍历了还要用这样一种迭代器模式来实现,只有等待工作经验的加深来进一步理解吧。
参考文档:
http://www.cnblogs.com/davidhhuan/p/4248206.html
http://blog.csdn.net/hguisu/article/details/7552841
http://www.phppan.com/2010/04/php-iterator-and-yii-cmapiterator/
PHP源码阅读笔记二十四 :iterator实现中当值为false时无法完成迭代的原因分析:http://www.phppan.com/2010/04/php-source-24-iterator-false-value/
0 0
- 设计模式入门-迭代器模式(php版)
- 设计模式入门-策略模式(php版)
- 设计模式入门-代理模式(php版)
- 设计模式入门-装饰器模式(php版)
- 设计模式入门-适配器模式(php版)
- 设计模式入门-模板方法模式(php版)
- 设计模式入门-状态模式(php版)
- php设计模式入门-观察者模式
- php设计模式入门-命令模式
- php设计模式入门-单例模式
- php设计模式入门-注册表模式
- php设计模式(迭代器模式)
- 设计模式入门:迭代器模式
- 设计模式(一):设计模式入门
- 慕课网----大话PHP设计模式 五(原型模式,装饰器模式,迭代器模式,代理模式)
- 设计模式入门之迭代器模式Iterator
- 设计模式入门-观察者模式(二)
- 设计模式入门(策略模式)
- AsyncHttpClient关于失败响应和网络超时的响应的处理
- avl树的C++实现
- MVC应用程序中管理(更新)上传的文件
- linux服务器进程监控及自动重启的方法
- BSS段、数据段、代码段、堆与栈
- 设计模式入门-迭代器模式(php版)
- Jvm工作原理学习笔记
- UIScrollView的属性总结
- 在Ubuntu系统下安装luci的步骤和注意事项
- HDU4500 小Q系列故事——屌丝的逆袭【模拟】
- VS2010下配置OpenMesh
- Apple Watch平台认知与产品设计
- 设计原则
- 数学之路-sas备忘(16)