欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP設(shè)計(jì)模式之迭代器模式的深入解析

 更新時(shí)間:2013年06月13日 15:59:21   作者:  
本篇文章是對(duì)PHP設(shè)計(jì)模式中的迭代器模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

迭代器(Iterator)模式,它在一個(gè)很常見(jiàn)的過(guò)程上提供了一個(gè)抽象:位于對(duì)象圖不明部分的一組對(duì)象(或標(biāo)量)集合上的迭代。迭代有幾種不同的具體執(zhí)行方法:在數(shù)組屬性,集合對(duì)象,數(shù)組,甚至一個(gè)查詢(xún)結(jié)果集之上迭代。

在對(duì)象的世界里,迭代器模式要維持類(lèi)似數(shù)組的功能,看作是一個(gè)非侵入性對(duì)象刻面(facet),Client類(lèi)往往分離自真實(shí)對(duì)象實(shí)現(xiàn),指iterator接口。只要有可能,我們可以給迭代器傳送一個(gè)引用,代替將來(lái)可能發(fā)生變化的具體或抽象類(lèi)。

參與者:
◆客戶(hù)端(Client):
引用迭代器模式的方法在一組值或?qū)ο笊蠄?zhí)行一個(gè)循環(huán)。
◆迭代器(Iterator):在迭代過(guò)程上的抽象,包括next(),isFinished(),current()等方法。
◆具體迭代器(ConcreteIterators):在一個(gè)特定的對(duì)象集,如數(shù)組,樹(shù),組合,集合等上實(shí)現(xiàn)迭代。
通過(guò)Traversable接口,PHP原生態(tài)支持迭代器模式,這個(gè)接口由Iterator和IteratorAggregate做了擴(kuò)展,這兩個(gè)子接口不僅是定義了一套標(biāo)準(zhǔn)的方法,每個(gè)Traversable對(duì)象都可以原封不動(dòng)地傳遞給foreach(),foreach是迭代器的主要客戶(hù)端,Iterator實(shí)現(xiàn)是真正的迭代器,而IteratorAggregate是有其它職責(zé)的Traversable對(duì)象,它通過(guò)getIterator()方法返回一個(gè)Iterator。

標(biāo)準(zhǔn)PHP庫(kù)是PHP中綁定的唯一通用目的面向?qū)ο髱?kù),定義了額外的接口和公用類(lèi)。OuterIterator實(shí)現(xiàn)裝飾一個(gè)Iterator,CachingIterator和LimitIterator是這個(gè)接口的兩個(gè)例子。

RecursiveIterator是Iterator接口為樹(shù)形結(jié)構(gòu)實(shí)現(xiàn)的一個(gè)擴(kuò)展,它定義了一組額外的方法檢查迭代中當(dāng)前元素的子對(duì)象是否存在。RecursiveArrayIterator和RecursiveDirectoryIterator是這個(gè)接口的實(shí)現(xiàn)示例,這些類(lèi)型的迭代器可以原樣使用,或是用一個(gè)RecursiveIteratorIterator橋接到一個(gè)普通的迭代器契約。這個(gè)OuterIterator實(shí)現(xiàn)將會(huì)根據(jù)構(gòu)造參數(shù)執(zhí)行深度優(yōu)先或廣度優(yōu)先遍歷。

使用RecursiveIteratorIterator時(shí),可以將其傳遞給foreach,請(qǐng)看后面的代碼示例,了解RecursiveIterators的不同用法和它們的超集Iterator。最后,SeekableIterators向契約添加了一個(gè)seek()方法,它可以用于移動(dòng)Iterator的內(nèi)部狀態(tài)到一個(gè)特定的迭代點(diǎn)。 

注意,迭代器是比對(duì)象集更好的抽象,因?yàn)槲覀兛梢宰孖nfiniteIterators,NoRewindIterators等,不用與普通數(shù)組陣列一致,因此,Iterator缺少count()函數(shù)等功能。

在PHP官方手冊(cè)中可以找到完整的SPL迭代器列表。得益于對(duì)PHP的強(qiáng)力支持,使用迭代器模式的大部分工作都包括在標(biāo)準(zhǔn)實(shí)現(xiàn)中,下面的代碼示例就利用了標(biāo)準(zhǔn)Iterator和RecursiveIterators的功能。

復(fù)制代碼 代碼如下:

    <?php
    /** 
     * Collection that wraps a numeric array. 
     * All five public methods are needed to implement 
     * the Iterator interface. 
     */ 
    class Collection implements Iterator 
    { 
 private $_content; 
 private $_index = 0; 

 public function __construct(array $content) 
 { 
     $this->_content = $content; 
 } 

 public function rewind() 
 { 
     $this->_index = 0; 
 } 

 public function valid() 
 { 
     return isset($this->_content[$this->_index]); 
 } 

 public function current() 
 { 
     return $this->_content[$this->_index]; 
 } 

 public function key() 
 { 
     return $this->_index; 
 } 

 public function next() 
 { 
     $this->_index++; 
 } 
    } 

    $array = array('A', 'B', 'C', 'D'); 
    echo "Collection: "; 
    foreach (new Collection($array) as $key => $value) { 
 echo "$key => $value. "; 
    } 
    echo "\n";
    /** 
     * Usually IteratorAggregate is the interface to implement. 
     * It has only one method, which must return an Iterator 
     * already defined as another class (e.g. ArrayIterator) 
     * Iterator gives a finer control over the algorithm, 
     * because all the hook points of Iterator' contract 
     * are available for implementation. 
     */ 
    class NumbersSet implements IteratorAggregate 
    { 
 private $_content; 

 public function __construct(array $content) 
 { 
     $this->_content = $content; 
 } 

 public function contains($number) 
 { 
     return in_array($number, $this->_content); 
 } 

 /** 
  * Only this method is necessary to implement IteratorAggregate. 
  * @return Iterator 
  */ 
 public function getIterator() 
 { 
     return new ArrayIterator($this->_content); 
 } 
    } 

    echo "NumbersSet: "; 
    foreach (new NumbersSet($array) as $key => $value) { 
 echo "$key => $value. "; 
    } 
    echo "\n";
    // let's play with RecursiveIterator implementations 
    $it = new RecursiveArrayIterator(array( 
 'A', 
 'B', 
 array( 
     'C', 
     'D' 
 ), 
 array( 
     array( 
  'E', 
  'F' 
     ), 
     array( 
  'G', 
  'H', 
  'I' 
     ) 
 ) 
    )); 
    // $it is a RecursiveIterator but also an Iterator, 
    // so it loops normally over the four elements 
    // of the array. 
    echo "Foreach over a RecursiveIterator: "; 
    foreach ($it as $value) { 
 echo $value; 
 // but RecursiveIterators specify additional 
 // methods to explore children nodes 
 $children = $it->hasChildren() ? '{Yes}' : '{No}'; 
 echo $children, ' '; 
    } 
    echo "\n"; 
    // we can bridge it to a different contract via 
    // a RecursiveIteratorIterator, whose cryptic name 
    // should be read as 'an Iterator that spans over 
    // a RecursiveIterator'. 
    echo "Foreach over a RecursiveIteratorIterator: "; 
    foreach (new RecursiveIteratorIterator($it) as $value) { 
 echo $value; 
    } 
    echo "\n";

相關(guān)文章

  • php基本函數(shù)匯總

    php基本函數(shù)匯總

    本文給大家匯總了16個(gè)常見(jiàn)的php基本函數(shù),涵蓋的面很廣,這里推薦給大家,希望對(duì)大家學(xué)習(xí)php能夠有所幫助。
    2015-07-07
  • php生成固定長(zhǎng)度純數(shù)字編碼的方法

    php生成固定長(zhǎng)度純數(shù)字編碼的方法

    這篇文章主要介紹了php生成固定長(zhǎng)度純數(shù)字編碼的方法,涉及php字符串與數(shù)組的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • php分割合并兩個(gè)字符串的函數(shù)實(shí)例

    php分割合并兩個(gè)字符串的函數(shù)實(shí)例

    這篇文章主要介紹了php分割合并兩個(gè)字符串的函數(shù),實(shí)例分析了php針對(duì)字符串操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • php 如何設(shè)置一個(gè)嚴(yán)格控制過(guò)期時(shí)間的session

    php 如何設(shè)置一個(gè)嚴(yán)格控制過(guò)期時(shí)間的session

    本篇文章主要介紹了php設(shè)置一個(gè)嚴(yán)格控制過(guò)期時(shí)間的session的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-05-05
  • 淺談PHP 閉包特性在實(shí)際應(yīng)用中的問(wèn)題

    淺談PHP 閉包特性在實(shí)際應(yīng)用中的問(wèn)題

    PHP5.3 新版本跟隨了很多新特性, 其中比較惹眼的特性之一就是支持了閉包。那么以后,我們也可以和那幫寫(xiě) Ruby、Javascript 等等“高科技語(yǔ)言”的家伙們一樣,寫(xiě)出非??岬拇a嗎?
    2009-10-10
  • 使用PHP下載CSS文件中的圖片的代碼

    使用PHP下載CSS文件中的圖片的代碼

    CSS文件中的圖片在以前不知道該如何下載,而現(xiàn)在卻可以使用php簡(jiǎn)單實(shí)現(xiàn)了,具體的如下,感興趣的朋友可以參考下
    2013-09-09
  • PHP 實(shí)現(xiàn) WebSocket 協(xié)議原理與應(yīng)用詳解

    PHP 實(shí)現(xiàn) WebSocket 協(xié)議原理與應(yīng)用詳解

    這篇文章主要介紹了PHP 實(shí)現(xiàn) WebSocket 協(xié)議,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了websocket協(xié)議原理、以及PHP具體應(yīng)用相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • php md5下16位和32位的實(shí)現(xiàn)代碼

    php md5下16位和32位的實(shí)現(xiàn)代碼

    PHP里MD5加密的16位和32位實(shí)現(xiàn)代碼,在網(wǎng)上一搜也有不少人有這方面的困惑,后來(lái)找到一個(gè)解決辦法,是正確的,就記錄下來(lái)
    2008-04-04
  • PHP結(jié)構(gòu)型模式之外觀模式

    PHP結(jié)構(gòu)型模式之外觀模式

    這篇文章主要介紹了PHP結(jié)構(gòu)型模式之外觀模式,外觀模式是一種結(jié)構(gòu)型模式,它提供了一個(gè)簡(jiǎn)單的接口,隱藏了系統(tǒng)的復(fù)雜性,為客戶(hù)端提供了一個(gè)簡(jiǎn)單的入口點(diǎn)
    2023-04-04
  • php獲得當(dāng)前的腳本網(wǎng)址

    php獲得當(dāng)前的腳本網(wǎng)址

    這篇文章介紹了php獲得當(dāng)前的腳本網(wǎng)址的方法,通過(guò)php服務(wù)器變量$_SERVER的簡(jiǎn)單判斷、轉(zhuǎn)換與輸出,實(shí)現(xiàn)獲取當(dāng)前網(wǎng)址的功能,需要的朋友可以參考一下
    2007-12-12

最新評(píng)論