亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

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

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

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

注意,迭代器是比對象集更好的抽象,因為我們可以讓InfiniteIterators,NoRewindIterators等,不用與普通數(shù)組陣列一致,因此,Iterator缺少count()函數(shù)等功能。

在PHP官方手冊中可以找到完整的SPL迭代器列表。得益于對PHP的強力支持,使用迭代器模式的大部分工作都包括在標(biāo)準(zhǔn)實現(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個常見的php基本函數(shù),涵蓋的面很廣,這里推薦給大家,希望對大家學(xué)習(xí)php能夠有所幫助。
    2015-07-07
  • php生成固定長度純數(shù)字編碼的方法

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論