婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析

PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析

熱門標(biāo)簽:南京3D地圖標(biāo)注 重慶外呼電銷系統(tǒng)多少錢 咸陽(yáng)電銷 正規(guī)電銷機(jī)器人系統(tǒng) 嘟嘟云外呼系統(tǒng) 南寧電話外呼系統(tǒng)線路 邢臺(tái)400電話辦理 濟(jì)源百應(yīng)電銷機(jī)器人聯(lián)系方式 辦理400電話哪家好點(diǎn)

本文實(shí)例講述了PHP設(shè)計(jì)模式入門之迭代器模式。分享給大家供大家參考,具體如下:

在深入研究這個(gè)設(shè)計(jì)模式之前,我們先來(lái)看一道面試題,來(lái)自鳥哥的博客,

題目是這樣的:

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

不使用迭代器模式很難實(shí)現(xiàn),先看實(shí)現(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接口來(lái)自php的spl類庫(kù),在寫完設(shè)計(jì)模式的相關(guān)文章之后,將會(huì)進(jìn)一步研究這個(gè)類庫(kù)。

另外在網(wǎng)上找到了一段yii框架中關(guān)于迭代器模式的實(shí)現(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è)計(jì)模式官方的定義是:使用迭代器模式來(lái)提供對(duì)聚合對(duì)象的統(tǒng)一存取,即提供一個(gè)外部的迭代器來(lái)對(duì)聚合對(duì)象進(jìn)行訪問和遍歷 , 而又不需暴露該對(duì)象的內(nèi)部結(jié)構(gòu)。又叫做游標(biāo)(Cursor)模式。

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

參考文檔:

https://www.jb51.net/article/184182.htm

https://www.jb51.net/article/185478.htm

https://www.jb51.net/article/185483.htm

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP設(shè)計(jì)模式之迭代器模式Iterator實(shí)例分析【對(duì)象行為型】
  • php設(shè)計(jì)模式之迭代器模式實(shí)例分析【星際爭(zhēng)霸游戲案例】
  • PHP設(shè)計(jì)模式之PHP迭代器模式講解
  • PHP設(shè)計(jì)模式之迭代器模式
  • PHP設(shè)計(jì)模式之迭代器模式的深入解析
  • PHP設(shè)計(jì)模式之迭代器模式的使用

標(biāo)簽:黃山 通遼 河南 南通 武漢 隴南 唐山 平頂山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析》,本文關(guān)鍵詞  PHP,設(shè)計(jì)模式,入門,之,迭代,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP設(shè)計(jì)模式入門之迭代器模式原理與實(shí)現(xiàn)方法分析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 青田县| 兰坪| 咸阳市| 新巴尔虎右旗| 六安市| 淮安市| 通海县| 辽阳市| 宁乡县| 会昌县| 江孜县| 绍兴市| 平果县| 鄄城县| 淮阳县| 夹江县| 吉木乃县| 广饶县| 西宁市| 阜南县| 台湾省| 阿拉善左旗| 竹山县| 永兴县| 江达县| 竹溪县| 佛坪县| 施秉县| 花莲市| 开远市| 望都县| 宜君县| 吉隆县| 东乡县| 兰州市| 定南县| 左云县| 定南县| 兴化市| 大悟县| 南投市|