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 相关文章推荐
初学者入门:细述PHP4的核心Zend
Sep 05 PHP
php htmlspecialchars()与shtmlspecialchars()函数的深入分析
Jun 05 PHP
PHP CodeBase:将时间显示为&quot;刚刚&quot;&quot;n分钟/小时前&quot;的方法详解
Jun 06 PHP
深入file_get_contents函数抓取内容失败的原因分析
Jun 25 PHP
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
Jun 25 PHP
PHP PDOStatement:bindParam插入数据错误问题分析
Nov 13 PHP
Laravel 4 初级教程之安装及入门
Oct 30 PHP
thinkphp3.2点击刷新生成验证码
Feb 16 PHP
PHP编程之设置apache虚拟目录
Jul 08 PHP
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
Apr 03 PHP
php apache开启跨域模式过程详解
Jul 08 PHP
PHP替换Word中变量并导出PDF图片的实现方法
Nov 26 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的日期与时间函数技巧
2008/04/24 PHP
php实现的简单数据库操作Model类
2016/11/16 PHP
firefox下jquery iframe刷新页面提示会导致重复之前动作
2012/12/17 Javascript
js获取键盘按键响应事件(兼容各浏览器)
2013/05/16 Javascript
js跳转页面方法实现汇总
2014/02/11 Javascript
js实现TAB切换对应不同颜色的代码
2015/08/31 Javascript
jquery实现可自动收缩的TAB网页选项卡代码
2015/09/06 Javascript
jQuery实现带玻璃流光质感的手风琴特效
2015/11/20 Javascript
js点击按钮实现水波纹效果代码(CSS3和Canves)
2016/09/15 Javascript
AngularJS中transclude用法详解
2016/11/03 Javascript
Ajax与服务器(JSON)通信实例代码
2016/11/05 Javascript
实现点击下箭头变上箭头来回切换的两种方法【推荐】
2016/12/14 Javascript
jQuery实现页面滚动时智能浮动定位
2017/01/08 Javascript
js实现淡入淡出轮播切换功能
2017/01/13 Javascript
BootStrapValidator初使用教程详解
2017/02/10 Javascript
Bootstrap Scrollspy源码学习
2017/03/02 Javascript
canvas轨迹回放功能实现
2017/12/20 Javascript
node前端模板引擎Jade之标签的基本写法
2018/05/11 Javascript
linux 后台运行node服务指令方法
2018/05/23 Javascript
使用JavaScript计算前一天和后一天的思路详解
2019/12/20 Javascript
vue中使用WX-JSSDK的两种方法(推荐)
2020/01/18 Javascript
详解如何在vue+element-ui的项目中封装dialog组件
2020/12/11 Vue.js
[00:28]DOTA2北京网鱼队选拔赛
2015/04/08 DOTA
[46:20]CHAOS vs Alliacne 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
python pdb调试方法分享
2014/01/21 Python
Python编程实现及时获取新邮件的方法示例
2017/08/10 Python
分析运行中的 Python 进程详细解析
2019/06/22 Python
Python如何爬取微信公众号文章和评论(基于 Fiddler 抓包分析)
2019/06/28 Python
Django 接收Post请求数据,并保存到数据库的实现方法
2019/07/12 Python
详解解决Python memory error的问题(四种解决方案)
2019/08/08 Python
python实现知乎高颜值图片爬取
2019/08/12 Python
一款纯css3实现的圆形旋转分享按钮旋转角度可自己调整
2014/09/02 HTML / CSS
BabyBjörn婴儿背带法国官网:BabyBjorn法国
2018/06/16 全球购物
文件中有一组整数,要求排序后输出到另一个文件中
2012/01/04 面试题
项目委托协议书(最新)
2014/09/13 职场文书
使用vue判断当前环境是安卓还是IOS
2022/04/12 Vue.js