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 相关文章推荐
WinXP + Apache +PHP5 + MySQL + phpMyAdmin安装全功略
Jul 09 PHP
PHP 数组基础知识小结
Aug 20 PHP
支持中文的php加密解密类代码
Nov 27 PHP
php $_SERVER windows系统与linux系统下的区别说明
Feb 14 PHP
PHP中你应该知道的require()文件包含的正确用法
Jun 12 PHP
php把数组值转换成键的方法
Jul 13 PHP
php解析base64数据生成图片的方法
Dec 06 PHP
php使用curl实现ftp文件下载功能
May 16 PHP
PHP4和PHP5版本下解析XML文档的操作方法实例分析
May 20 PHP
Ubuntu彻底删除PHP7.0的方法
Jul 27 PHP
使用laravel的Eloquent模型如何获取数据库的指定列
Oct 17 PHP
PHP实现简单的协程任务调度demo示例
Feb 01 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中文件上传的一个问题
2010/09/04 PHP
PHP模糊查询的实现方法(推荐)
2016/09/06 PHP
document.documentElement &amp;&amp; document.documentElement.scrollTop
2007/12/01 Javascript
js函数使用技巧之 setTimeout(function(){},0)
2009/02/09 Javascript
jQuery的运行机制和设计理念分析
2011/04/05 Javascript
Ajax执行顺序流程及回调问题分析
2012/12/10 Javascript
为什么要在引入的css或者js文件后面加参数的详细讲解
2013/05/03 Javascript
基于JavaScript实现表单密码的隐藏和显示出来
2016/03/02 Javascript
AngularJs学习第八篇 过滤器filter创建
2016/06/08 Javascript
JS获取中文拼音首字母并通过拼音首字母快速查找页面内对应中文内容的方法【附demo源码】
2016/08/19 Javascript
JavaScript获取服务器时间的方法详解
2016/12/11 Javascript
js实现从左向右滑动式轮播图效果
2017/07/07 Javascript
基于angular-utils-ui-breadcrumbs使用心得(分享)
2017/11/03 Javascript
JS写XSS cookie stealer来窃取密码的步骤详解
2017/11/20 Javascript
JavaScript中判断为整数的多种方式及保留两位小数的方法
2019/09/09 Javascript
谈谈node.js中的模块系统
2020/09/01 Javascript
JavaScript 实现继承的几种方式
2021/02/19 Javascript
[10:28]2018DOTA2国际邀请赛寻真——VGJ.S寻梦之路
2018/08/15 DOTA
使用Pyrex来扩展和加速Python程序的教程
2015/04/13 Python
Python+微信接口实现运维报警
2016/08/27 Python
【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
2016/11/19 Python
python清除字符串前后空格函数的方法
2018/10/21 Python
[原创]Python入门教程4. 元组基本操作
2018/10/31 Python
Python3实现取图片中特定的像素替换指定的颜色示例
2019/01/24 Python
Python3数字求和的实例
2019/02/19 Python
Django自定义模板过滤器和标签的实现方法
2019/08/21 Python
matplotlib jupyter notebook 图像可视化 plt show操作
2020/04/24 Python
Python3.7下安装pyqt5的方法步骤(图文)
2020/05/12 Python
Pycharm 跳转回之前所在页面的操作
2021/02/05 Python
HTML5 Canvas绘制五星红旗
2016/05/04 HTML / CSS
美国知名的旅游网站:OneTravel
2018/10/09 全球购物
亮化工程实施方案
2014/03/17 职场文书
单位作风建设自查报告
2014/10/23 职场文书
付款证明模板
2015/06/19 职场文书
《秋天的雨》教学反思
2016/02/19 职场文书
python神经网络ResNet50模型
2022/05/06 Python