zoukankan      html  css  js  c++  java
  • EmptyIterator并非废材

    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…接收异常处理。
  • 相关阅读:
    (find) nyoj5-Binary String Matching
    (replace find)nyoj113-字符串替换
    (去重 sort)nyoj8-一种排序
    (模拟 打好基础) nyoj1363-计划日
    一种颜色怎么搭配
    动态识别类型
    swift与OC混合编程
    IOS动画效果
    NSAlert
    NSButton文本颜色设置和Action两种状态行为
  • 原文地址:https://www.cnblogs.com/sumsung753/p/5123089.html
Copyright © 2011-2022 走看看