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和Mysqlweb应用开发核心技术-第1部分 Php基础-2 php语言介绍
Jul 03 PHP
PHP不用第三变量交换2个变量的值的解决方法
Jun 02 PHP
php漏洞之跨网站请求伪造与防止伪造方法
Aug 15 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 PHP
PHP中HTML标签过滤技巧
Jan 07 PHP
destoon出现验证码不显示时的紧急处理方法
Aug 22 PHP
Linux系统下php获得系统分区信息的方法
Mar 30 PHP
FastCGI 进程意外退出造成500错误
Jul 26 PHP
详解PHP中websocket的使用方法
Sep 15 PHP
PHP 中常量的知识整理
Apr 14 PHP
PHP面向对象程序设计中的self、static、parent关键字用法分析
Aug 14 PHP
PHP7数组的底层实现示例
Aug 25 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
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
Smarty模板类内部原理实例分析
2019/07/03 PHP
JavaScript在IE和Firefox浏览器下的7个差异兼容写法小结
2010/06/18 Javascript
JavaScript高级程序设计 错误处理与调试学习笔记
2011/09/10 Javascript
JQuery拖拽元素改变大小尺寸实现代码
2012/12/10 Javascript
jQuery :first选择器使用介绍
2013/08/09 Javascript
JavaScript显示表单内元素数量的方法
2015/04/02 Javascript
JS三级可折叠菜单实现方法
2016/02/29 Javascript
JS获得多个同name 的input输入框的值的实现方法
2017/01/09 Javascript
轻松理解JavaScript之AJAX
2017/03/15 Javascript
AngularJS中$http的交互问题
2017/03/29 Javascript
Underscore之Array_动力节点Java学院整理
2017/07/10 Javascript
vue-cli3脚手架的配置及使用教程
2018/08/28 Javascript
实例讲解vue源码架构
2019/01/24 Javascript
vue-router 起步步骤详解
2019/03/26 Javascript
js实现简单页面全屏
2019/09/17 Javascript
JS实现电商商品展示放大镜特效
2020/01/07 Javascript
JavaScript中reduce()的5个基本用法示例
2020/07/19 Javascript
使用Python爬取最好大学网大学排名
2018/02/24 Python
python如何为被装饰的函数保留元数据
2018/03/21 Python
教你使用python画一朵花送女朋友
2018/03/29 Python
在python中获取div的文本内容并和想定结果进行对比详解
2019/01/02 Python
python查找重复图片并删除(图片去重)
2019/07/16 Python
HTML5的语法变化介绍
2013/08/13 HTML / CSS
HTML5拖放API实现拖放排序的实例代码
2017/05/11 HTML / CSS
德国黑胶唱片、街头服装及运动鞋网上商店:HHV
2018/08/24 全球购物
优纳科技软件测试面试题
2012/05/15 面试题
大学生自荐书范文
2013/12/10 职场文书
社会学专业学生职业规划书
2014/02/07 职场文书
对教师的评语
2014/04/28 职场文书
走进敬老院活动总结
2014/07/10 职场文书
网吧消防安全责任书
2014/07/29 职场文书
手机销售员岗位职责
2015/04/11 职场文书
Python爬虫基础之简单说一下scrapy的框架结构
2021/06/26 Python
一小时迅速入门Mybatis之bind与多数据源支持 Java API
2021/09/15 Javascript
解决vue-router的beforeRouteUpdate不能触发
2022/04/14 Vue.js