溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

講解PHP設(shè)計模式入門之迭代器模式原理與實現(xiàn)方法

發(fā)布時間:2021-03-08 16:24:50 來源:億速云 閱讀:126 作者:TREX 欄目:開發(fā)技術(shù)

這篇文章主要介紹“講解PHP設(shè)計模式入門之迭代器模式原理與實現(xiàn)方法”,在日常操作中,相信很多人在講解PHP設(shè)計模式入門之迭代器模式原理與實現(xiàn)方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”講解PHP設(shè)計模式入門之迭代器模式原理與實現(xiàn)方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

在深入研究這個設(shè)計模式之前,我們先來看一道面試題

題目是這樣的:

使對象可以像數(shù)組一樣進行foreach循環(huán),要求屬性必須是私有。

不使用迭代器模式很難實現(xiàn),先看實現(xiàn)的代碼:

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類庫,在寫完設(shè)計模式的相關(guān)文章之后,將會進一步研究這個類庫。

另外在網(wǎng)上找到了一段yii框架中關(guān)于迭代器模式的實現(xiàn)代碼:

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 />';
}

關(guān)于迭代器設(shè)計模式官方的定義是:使用迭代器模式來提供對聚合對象的統(tǒng)一存取,即提供一個外部的迭代器來對聚合對象進行訪問和遍歷 , 而又不需暴露該對象的內(nèi)部結(jié)構(gòu)。又叫做游標(Cursor)模式。

好吧,我不是很能理解。為什么明明數(shù)組已經(jīng)可以用foreach來遍歷了還要用這樣一種迭代器模式來實現(xiàn),只有等待工作經(jīng)驗的加深來進一步理解吧。

到此,關(guān)于“講解PHP設(shè)計模式入門之迭代器模式原理與實現(xiàn)方法”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI