迭代器模式:将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构*/

来源:互联网 发布:ubuntu显示隐藏文件 编辑:程序博客网 时间:2024/06/05 15:35
<!-- 迭代器:是遍历集合的成熟模式-->
<?php
/*
迭代器模式:将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构*/
interface Iterators{
public function First();
public function Pre();
public function Next();
public function End();
public function CurrentItem();
}
?>


<?php
require_once "Iterators.php";
class Iteratorobj implements Iterators{
public $objects;
public $current = 0;
public function __construct($objects)
{
$this->objects = $objects;
}


public function First()
{
return $this->objects[0];
}


public function Pre()
{
$this->current--;
if($this->current < 0 ){
return false;
}else{
return $this->objects[$this->current];
}
}


public function Next()
{
$this->current++;
if($this->current <= count($this->objects))
{
return $this->objects[$this->current];
}
return false;
}


public function End()
{
return $this->objects[count($this->objects)-1];
}




public function CurrentItem()
{
return $this->objects[$this->current];
}
}
?>




<?php
require_once "Iteratorobj.php";
$arr = array('hello',"world","say");
$obj = new Iteratorobj($arr);
echo $obj->First()."<br>";
echo $obj->End()."<br>";
echo $obj->Next()."<br>";
?>



阅读全文
0 0
原创粉丝点击