温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-03-08 16:24:50 来源:亿速云 阅读:119 作者:TREX 栏目:开发技术

这篇文章主要介绍“讲解PHP设计模式入门之迭代器模式原理与实现方法”,在日常操作中,相信很多人在讲解PHP设计模式入门之迭代器模式原理与实现方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”讲解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设计模式入门之迭代器模式原理与实现方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI