• PHP设计模式—迭代器模式


    定义:

    迭代器模式(Iterator):提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。

     

    结构:

    • Iterator:迭代器接口,用于定义得到开始对象、得到下一个对象、判断是否到有效、当前对象等抽象方法,统一接口,目前PHP已经集成有该类。
    • IteratorAggregate:容器接口,目前PHP已经集成有该类。
    • ConcreteAggregate:具体容器类,继承IteratorAggregate。
    • ConcreteIterator:具体迭代器类,继承Iterator。
    • Client:客户端代码。

     

    代码实例:

    /**
     * IteratorAggregate 源码
     * Interface to create an external Iterator.
     * @link https://php.net/manual/en/class.iteratoraggregate.php
     */
    interface IteratorAggregate extends Traversable {
    
        /**
         * Retrieve an external iterator
         * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
         * @return Traversable An instance of an object implementing <b>Iterator</b> or
         * <b>Traversable</b>
         * @since 5.0.0
         */
        public function getIterator();
    }
    
    /**
     * Iterator源码
     * Interface for external iterators or objects that can be iterated
     * themselves internally.
     * @link https://php.net/manual/en/class.iterator.php
     */
    interface Iterator extends Traversable {
    
        /**
         * Return the current element
         * @link https://php.net/manual/en/iterator.current.php
         * @return mixed Can return any type.
         * @since 5.0.0
         */
        public function current();
    
        /**
         * Move forward to next element
         * @link https://php.net/manual/en/iterator.next.php
         * @return void Any returned value is ignored.
         * @since 5.0.0
         */
        public function next();
    
        /**
         * Return the key of the current element
         * @link https://php.net/manual/en/iterator.key.php
         * @return mixed scalar on success, or null on failure.
         * @since 5.0.0
         */
        public function key();
    
        /**
         * Checks if current position is valid
         * @link https://php.net/manual/en/iterator.valid.php
         * @return boolean The return value will be casted to boolean and then evaluated.
         * Returns true on success or false on failure.
         * @since 5.0.0
         */
        public function valid();
    
        /**
         * Rewind the Iterator to the first element
         * @link https://php.net/manual/en/iterator.rewind.php
         * @return void Any returned value is ignored.
         * @since 5.0.0
         */
        public function rewind();
    }
    
    
    /**
     * 具体聚集类
     * Class ConcreteAggregate
     */
    class ConcreteAggregate implements IteratorAggregate
    {
        private $data = [];
    
        /**
         * 往迭代器里面添加数据
         */
        public function add($name)
        {
            $this->data[] = $name;
        }
    
        /**
         * 获取迭代器
         * @return ConcreteIterator|Traversable
         */
        public function getIterator()
        {
            // TODO: Implement getIterator() method.
            return new ConcreteIterator($this->data);
        }
    }
    
    
    /**
     * 具体迭代器类
     * Class ConcreteIterator
     */
    class ConcreteIterator implements Iterator
    {
        private $key = 0;
        private $data = [];
    
        public function __construct($data)
        {
            $this->data = $data;
            $this->key = 0;
        }
    
        /**
         * 返回当前元素
         */
        public function current()
        {
            // TODO: Implement current() method.
            return $this->data[$this->key];
        }
    
        /**
         * 前进到下一个元素
         */
        public function next()
        {
            // TODO: Implement next() method.
            return $this->key++;
        }
    
        /**
         * 返回当前元素的键
         */
        public function key()
        {
            // TODO: Implement key() method.
            return $this->key;
        }
    
        /**
         * 检查当前位置是否有效
         */
        public function valid()
        {
            // TODO: Implement valid() method.
            return isset($this->data[$this->key]);
        }
    
    
        /**
         * 将Iterator倒退到第一个元素
         */
        public function rewind()
        {
            // TODO: Implement rewind() method.
            return $this->key = 0;
        }
    }
    
    
    // 客户端调用
    $concreteAggregate = new ConcreteAggregate();
    $concreteAggregate->add('张三');
    $concreteAggregate->add('李四');
    $concreteAggregate->add('王五');
    
    $concreteIterator = $concreteAggregate->getIterator();
    foreach ($concreteIterator as $concrete) {
        echo $concrete . "<br>";
    }
    
    
    // 结果
    张三
    李四
    王五
  • 相关阅读:
    C++ 类初始化的顺序
    Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
    Find out all the elements in A and B such that the A[i]B[j]=C[k]
    顺时针打印数组,美丽版
    Given a string S, find the longest palindromic substring in S.
    很多机器,每台机器上都有很一部分数据,如何输出分布在所有机器上的所有数据的median
    C++'s mutable and conceptual constness
    上20阶楼梯,可以一次迈1,2,3步,请问有多少种上法?
    Count smaller elements on right side in an array.
    一排房子,连续填色,成本最低的问题
  • 原文地址:https://www.cnblogs.com/woods1815/p/14031533.html
Copyright © 2020-2023  润新知