您的位置:首页 > 职场人生

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

2017-07-31 13:52 483 查看
<!-- 迭代器:是遍历集合的成熟模式-->

<?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>";

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐