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 相关文章推荐
PHP脚本的10个技巧(2)
Oct 09 PHP
在Windows版的PHP中使用ADO
Oct 09 PHP
PHP 分页类(模仿google)-面试题目解答
Sep 13 PHP
PHP操作mysql函数详解,mysql和php交互函数
May 19 PHP
PHP 多维数组的排序问题 根据二维数组中某个项排序
Nov 09 PHP
PHP系列学习之日期函数使用介绍
Aug 18 PHP
php删除字符串末尾子字符,删除开始字符,删除两端字符(实现代码)
Jun 27 PHP
php获取字段名示例分享
Mar 03 PHP
php include类文件超时问题处理
Feb 06 PHP
如何使用PHP对网站验证码进行破解
Sep 17 PHP
thinkPHP5框架分页样式类完整示例
Sep 01 PHP
PHP pthreads v3使用中的一些坑和注意点分析
Feb 21 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操作memcache缓存方法分享
2015/06/03 PHP
用JavaScript对JSON进行模式匹配 (Part 2 - 实现)
2010/07/17 Javascript
JQuery调webservice实现邮箱验证(检测是否可用)
2013/05/21 Javascript
JS实现拖动示例代码
2013/11/01 Javascript
jQuery中:submit选择器用法实例
2015/01/03 Javascript
jquery实现textarea输入框限制字数的方法
2015/01/15 Javascript
NodeJs——入门必看攻略
2016/06/27 NodeJs
js浏览器滚动条卷去的高度scrolltop(实例讲解)
2017/07/07 Javascript
vue-router+vuex addRoutes实现路由动态加载及菜单动态加载
2017/09/28 Javascript
vue中如何使用ztree
2018/02/06 Javascript
vue拖拽组件使用方法详解
2018/12/01 Javascript
layui对工具条进行选择性的显示方法
2019/09/19 Javascript
微信小程序入门之绘制时钟
2020/10/22 Javascript
[48:45]Ti4 循环赛第二日 NEWBEE vs EG
2014/07/11 DOTA
python实现识别相似图片小结
2016/02/22 Python
Python 正则表达式入门(初级篇)
2016/12/07 Python
Python实现的朴素贝叶斯分类器示例
2018/01/06 Python
python中abs&amp;map&amp;reduce简介
2018/02/20 Python
解决Tensorflow使用pip安装后没有model目录的问题
2018/06/13 Python
python/sympy求解矩阵方程的方法
2018/11/08 Python
django开发post接口简单案例,获取参数值的方法
2018/12/11 Python
利用arcgis的python读取要素的X,Y方法
2018/12/22 Python
pandas数据集的端到端处理
2019/02/18 Python
python调用外部程序的实操步骤
2019/03/04 Python
python常用库之NumPy和sklearn入门
2019/07/11 Python
分享8点超级有用的Python编程建议(推荐)
2019/10/13 Python
django框架ModelForm组件用法详解
2019/12/11 Python
matlab、python中矩阵的互相导入导出方式
2020/06/01 Python
表达自我的市场:Society6
2018/08/01 全球购物
八年级美术教学反思
2014/02/02 职场文书
《孔繁森》教学反思
2014/04/17 职场文书
超市周年庆活动方案
2014/08/16 职场文书
2014年高中生自我评价范文
2014/09/26 职场文书
围城读书笔记
2015/06/26 职场文书
2021-4-3课程——SQL Server查询【2】
2021/04/05 SQL Server
JAVA springCloud项目搭建流程
2022/05/11 Java/Android