ホームページ  >  記事  >  バックエンド開発  >  デザインパターン入門 - イテレータパターン(php版)

デザインパターン入門 - イテレータパターン(php版)

WBOY
WBOYオリジナル
2016-08-08 09:27:08784ブラウズ

このデザイン パターンを詳しく説明する前に、まず、Niao 兄弟のブログからのインタビューの質問を見てみましょう。

質問は次のようなものです:

オブジェクトが配列のように foreach ループを実行できるようにするには、必要なプロパティがプライベートである必要があります。

イテレータ パターンを使用せずに実装するのは困難です。まず実装コードを見てみましょう:

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 &#39;Sample.php&#39;;

$arr = new Sample([&#39;max&#39;, &#39;ben&#39;, &#39;will&#39;]); 

foreach ($arr as $k=>$v){
    echo $k."-".$v."<br />";
}

Iterator インターフェイスは PHP の spl クラス ライブラリから来ています。デザイン パターンに関する関連記事を書いた後、このクラス ライブラリについてさらに勉強します。

さらに、インターネット上で Yii フレームワークのイテレータ パターンに関する実装コードを見つけました:

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

イテレータ設計パターンの正式な定義は次のとおりです: イテレータ パターンを使用して集約オブジェクトへの統合アクセスを提供します。つまり、外部イテレータは、オブジェクトの内部構造を公開することなく、集約オブジェクトにアクセスし、走査するために使用されます。カーソルモードとも呼ばれます。

そうですね、よく分かりません。 foreach を使用して配列を走査できるにもかかわらず、なぜそのようなイテレータ モードを使用して実装する必要があるのでしょうか? さらに理解するには、作業経験が深まるのを待つしかありません。

参考資料:

http://www.cnblogs.com/davidhhuan/p/4248206.html

http://blog.csdn.net/hguisu/article/details/7552841

http:// www.phppan.com/2010/04/php-iterator-and-yii-cmapiterator/

PHPソースコード読書メモその24:イテレータ実装で値がfalseの場合に反復が完了できない原因の分析: http://www.phppan.com/2010/04/php-source-24-iterator-false-value/


以上、デザインパターン入門 - イテレータパターン(php版)について、関連内容も含めて紹介しましたので、PHPチュートリアルに興味のある方の参考になれば幸いです。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。