PHP迭代器模式(引用SPL实现)

现在有这么两个类,Department部门类、Employee员工类:

//部门类
class Department{
    private $_name;
    private $_employees;
    function __construct($name){
        $this->_name = $name;
        $this->employees = array();
    }
    function addEmployee(Employee $e){
        $this->_employees[] = $e;
        echo "员工{$e->getName()}被分配到{$this->_name}中去";
    }
}
//员工类
class Employee{
    private $_name;
    function __construct($name){
        $this->_name = $name;
    }
    function getName(){
        return $this->_name;
    }
}

//应用:

$lsgo = new Department('LSGO实验室');
$e1 = new Employee("小锦");
$e2 = new Employee("小猪");
$lsgo->addEmployee($e1);
$lsgo->addEmployee($e2);

好了,现在LSGO实验室已经有两个部员了,现在我想把全部的部员都列出来,就是用循环来获取部门的每个员工的详情。

在这里我们用PHP中的SPL标准库提供的迭代器来实现。

《大话设计模式》中如是说:
迭代器模式:迭代器模式是遍历集合的成熟模式,迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构。

迭代器模式的作用简而言之:是使所有复杂数据结构的组件都可以使用循环来访问

假如我们的对象要实现迭代,我们使这个类实现 Iterator(SPL标准库提供),这是一个迭代器接口,为了实现该接口,我们必须实现以下方法:

  1. current(),该函数返回当前数据项
  2. key(),该函数返回当前数据项的键或者该项在列表中的位置
  3. next(),该函数使数据项的键或者位置前移
  4. rewind(),该函数重置键值或者位置
  5. valid(),该函数返回 bool 值,表明当前键或者位置是否指向数据值

实现了 Iterator 接口和规定的方法后,PHP就能够知道该类类型的对象需要迭代。

我们使用这种方式重构 Department 类:

class Department implements Iterator
{
    private $_name;
    private $_employees;
    private $_position;//标志当前数组指针位置

    function __construct($name)
    {
        $this->_name = $name;
        $this->employees = array();
        $this->_position = 0;
    }

    function addEmployee(Employee $e)
    {
        $this->_employees[] = $e;
        echo "员工{$e->getName()}被分配到{$this->_name}中去";
    }

    //实现 Iterator 接口要求实现的方法
    function current()
    {
        return $this->_employees[$this->_position];
    }

    function key()
    {
        return $this->_position;
    }

    function next()
    {
        $this->_position++;
    }

    function rewind()
    {
        $this->_position = 0;
    }

    function valid()
    {
        return isset($this->_employees[$this->_position]);
    }
}

//Employee 类同前

//应用:

$lsgo = new Department('LSGO实验室');
$e1 = new Employee("小锦");
$e2 = new Employee("小猪");
$lsgo->addEmployee($e1);
$lsgo->addEmployee($e2);

echo "LSGO实验室部员情况:";
//这里其实遍历的$_employee
foreach($lsgo as $val){
    echo "部员{$val->getName()}";
}

附加:

假如现在我们想要知道该部门有几个员工,如果是数组的话,一个 count() 函数就 ok 了,那么我们能不能像上面那样把对象当作数组来处理?SPL标准库中提供了 Countable 接口供我们使用:

class Department implements Iterator,Countable{
    //前面同上

    //实现Countable中要求实现的方法
    function count(){
        return count($this->_employees);
    }
}

//应用:

echo "员工数量:";
echo count($lsgo);

猜你喜欢

转载自www.linuxidc.com/Linux/2017-05/143597.htm