PHP设计模式入门之迭代器模式原理与实现方法分析


Posted in PHP onApril 26, 2020

本文实例讲述了PHP设计模式入门之迭代器模式。分享给大家供大家参考,具体如下:

在深入研究这个设计模式之前,我们先来看一道面试题,来自鸟哥的博客,

题目是这样的:

使对象可以像数组一样进行foreach循环,要求属性必须是私有。

不使用迭代器模式很难实现,先看实现的代码:

sample.php

<?php
class 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

<?php
require '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来遍历了还要用这样一种迭代器模式来实现,只有等待工作经验的加深来进一步理解吧。

参考文档:

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
一个用于网络的工具函数库
Oct 09 PHP
PHP的面试题集,附我的答案和分析(一)
Nov 19 PHP
CodeIgniter php mvc框架 中国网站
May 26 PHP
php db类库进行数据库操作
Mar 19 PHP
PHP 程序员应该使用的10个组件
Oct 31 PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
Nov 01 PHP
PHP Directory 函数的详解
Mar 07 PHP
php中将html中的br换行符转换为文本输入中的换行符
Mar 26 PHP
php获取一个变量的名字的方法
Sep 05 PHP
PHP模拟asp中response类实现方法
Aug 08 PHP
基于php中echo用逗号和用点号的区别详解
Jan 23 PHP
PHP中__set()实例用法和基础讲解
Jul 23 PHP
PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例
Apr 26 #PHP
PHP设计模式之迭代器模式Iterator实例分析【对象行为型】
Apr 26 #PHP
Yii Framework框架开发微信公众平台示例
Apr 26 #PHP
PHP随机生成中文段落示例【测试网站内容时使用】
Apr 26 #PHP
PHP过滤器 filter_has_var() 函数用法实例分析
Apr 23 #PHP
PHP优化之批量操作MySQL实例分析
Apr 23 #PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
Apr 23 #PHP
You might like
PHP 数据结构 算法描述 冒泡排序 bubble sort
2011/07/10 PHP
PHP新手用的Insert和Update语句构造类
2012/03/31 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
2016/11/18 PHP
弹出模态框modal的实现方法及实例
2017/09/19 PHP
php实现的生成迷宫与迷宫寻址算法完整实例
2017/11/06 PHP
PHP的PDO大对象(LOBs)
2019/01/27 PHP
Javascript倒计时页面跳转实例小结
2013/09/11 Javascript
javascript圆盘抽奖程序实现原理和完整代码例子
2014/06/03 Javascript
处理文本部分内容的TextRange对象应用实例
2014/07/29 Javascript
详解JS函数重载
2014/12/04 Javascript
JavaScript跨浏览器获取页面中相同class节点的方法
2015/03/03 Javascript
AngularJS中的DOM操作用法分析
2016/11/04 Javascript
vue v-on监听事件详解
2017/05/17 Javascript
angularJs中datatable实现代码
2017/06/03 Javascript
input file样式修改以及图片预览删除功能详细概括(推荐)
2017/08/17 Javascript
解决axios发送post请求返回400状态码的问题
2018/08/11 Javascript
Nodejs Express 通过log4js写日志到Logstash(ELK)
2018/08/30 NodeJs
jQuery 点击获取验证码按钮及倒计时功能
2018/09/20 jQuery
bootstrap table合并行数据并居中对齐效果
2018/10/17 Javascript
使用pkg打包Node.js应用的方法步骤
2018/10/19 Javascript
vue自定义指令的创建和使用方法实例分析
2018/12/04 Javascript
vue19 组建 Vue.extend component、组件模版、动态组件 的实例代码
2019/04/04 Javascript
javascript判断一个变量是数组还是对象
2019/04/10 Javascript
javascript之分片上传,断点续传的实际项目实现详解
2019/09/05 Javascript
微信小程序实现列表左右滑动
2020/11/19 Javascript
将Python代码打包为jar软件的简单方法
2015/08/04 Python
python中urllib.unquote乱码的原因与解决方法
2017/04/24 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2018/06/04 Python
Python常见MongoDB数据库操作实例总结
2018/07/24 Python
python程序如何进行保存
2020/07/03 Python
Strawberrynet草莓网新加坡站:护肤、彩妆、香水及美发产品
2018/08/31 全球购物
娇韵诗法国官网:Clarins法国
2019/01/29 全球购物
New Balance俄罗斯官方网上商店:购买运动鞋
2020/03/02 全球购物
社会保险接收函
2014/01/12 职场文书
财务管理专业毕业生求职信
2014/06/02 职场文书
导游词之广州陈家祠
2019/10/21 职场文书