《PHP應(yīng)用:PHP設(shè)計(jì)模式之迭代器模式》要點(diǎn):
本文介紹了PHP應(yīng)用:PHP設(shè)計(jì)模式之迭代器模式,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP實(shí)例在不需要了解內(nèi)部實(shí)現(xiàn)的前提下,遍歷一個(gè)聚合對(duì)象的內(nèi)部元素而又不暴露該對(duì)象的內(nèi)部表示,這就是PHP迭代器模式的定義.
PHP實(shí)例適用場(chǎng)景:
訪問(wèn)一個(gè)聚合對(duì)象的內(nèi)容而無(wú)需暴露它的內(nèi)部表示
支持對(duì)聚合對(duì)象的多種遍歷
為遍歷不同的聚合結(jié)構(gòu)提供一個(gè)統(tǒng)一的接口
PHP實(shí)例迭代器模式實(shí)例:
PHP實(shí)例
<?php
class ConcreteIterator implements Iterator{
private $position = 0;
private $arr;
function __construct(array $arr){
$this->arr = $arr;
}
function rewind(){
$this->position = 0;
}
function current(){
return $this->arr[$this->position];
}
function key(){
return $this->position;
}
function next(){
++$this->position;
}
function valid(){
return isset($this->arr[$this->position]);
}
}
$arr = array('xiao hong','xiao ming','xiaohua');
$concreteIterator = new ConcreteIterator($arr);
foreach ($concreteIterator as $key => $value) {
echo $key."=>".$value."\n";
}
PHP實(shí)例以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)PHP設(shè)計(jì)模式有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/6127.html