PHP設計模式—迭代器模式

 

定義:

迭代器模式(Iterator):提供一種方法順序訪問一個聚合對象中各個元素,而又不暴露該對象的內部表示。php

 

結構:

  • 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>";
}


// 結果
張三
李四
王五
相關文章
相關標籤/搜索