首页 > 其他 > 详细

EmptyIterator并非废材

时间:2016-01-12 06:28:38      阅读:166      评论:0      收藏:0      [点我收藏+]

PHP PSL标准库提供了一套非常实用的迭代器,ArrayIterator用以迭代数组,CachingIterator迭代缓存,DirectoryIterator迭代目录…,各种迭代器各有所用。但仔细看下来竟然还有一个EmptyIterator。根据设计说明,The EmptyIterator class for an empty iterator. 它就是用来做空迭代的。

这真让人感到有点费解,空迭代的意义何在?

答案在这里,它就是用来放在需要空迭代的场合。在stackoverflow中,举了一个很恰当的例子:

interface Animal {
    public function makeSound();
}

class Dog implements Animal {
    public function makeSound() {
        echo "WOOF!";
    }
}

class Cat implements Animal {
    public function makeSound() {
        echo "MEOW!";
    }
}

class NullAnimal implements Animal { // Null Object Pattern Class
    public function makeSound() {
    }
}

$animalType = ‘donkey‘;
$animal;

switch($animalType) {
    case ‘dog‘ :
        $animal = new Dog();
        break;
    case ‘cat‘ :
        $animal = new Cat();
        break;
    default :
        $animal = new NullAnimal();
}
$animal->makeSound(); // won‘t make any sound bcz animal is ‘donkey‘

在上例中,当animal的类型不为列举的任何一个时,它提供一个“不产生任何声音”的NullAnimal对象,它使无论animal type是什么,animal总是属于Animal类的一种。这就让$animal有了归属的意义(总是属于Animal的类型)。

在迭代器当中,你可以遍历的范围限定在[DirectoryIterator,FilesystemIterator,…],当提供的类型指示不在限制范围之内时,你可以强制返回一个EmptyIterator,这样,后续的操作就不必将不合格的类型作错误或异常处理,这样做的好处是使操作产生连贯性,而不会中断。

这个策略在设计模式中经常使用。

$inputType = ‘some‘;
switch ($inputType) {
case ‘file‘:
$iterate = new FilesystemIterator(__FILE__);
break;
case ‘dir‘:
$iterate = new DirectoryIterator(__DIR__);
break;
default:
$iterate = new EmptyIterator();
}
do {
// do some codes
} while ($iterate->valid());
// or
for ($iterate->rewind(); $iterate->valid(); $iterate->next()) {
$key = $iterate->key();
$current = $iterate->current();
    // other codes
}
EmptyIterator继承自Iterator接口,提供了迭代器的一些判断方法,使得它可以像其它迭代器一样遍历操作,除了返回为空或false。
注意,因为是空,所以尝试对EmptyIterator操作current或next方法会抛出Exception。
如果打算这么做,最好使用try…catch…接收异常处理。

EmptyIterator并非废材

原文:http://www.cnblogs.com/sumsung753/p/5123089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!