迭代器模式及其php實現(Yii架構)

來源:互聯網
上載者:User
迭代器模式是一種行為型模式,它是一種最簡單也最常見的設計模式。它可以讓使用者透過特定的介面巡訪容器中的每一個元素而不用瞭解底層的實際操作。

適用性

在希望利用語言本身的遍曆函數便利自訂結構時,例如PHP中的foreach函數

類圖

PHP執行個體

<?phpclass sample implements Iterator {   private $_items ;   public function __construct(&$data) {       $this->_items = $data;   }   public function current() {       return current($this->_items);   }   public function next() {       next($this->_items);      }   public function key() {       return key($this->_items);   }   public function rewind() {       reset($this->_items);   }   public function valid() {                                                                                     return ($this->current() !== FALSE);   }}// client$data = array(1, 2, 3, 4, 5);$sa = new sample($data);foreach ($sa AS $key => $row) {   echo $key, ' ', $row, '<br />';}?>

在Yii架構中的實現:

在Yii架構中的我們可以看到其迭代器的實現,在collections目錄下的CMapIterator.php檔案中,其實現如下:

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

這與之前的簡單實現相比,其位置的變化是通過控制key來實現的,這種實現的作用是為了避免false作為數組值時無法迭代。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.