Home > Backend Development > PHP Tutorial > PHP Design Patterns - Iterator Pattern_PHP Tutorial

PHP Design Patterns - Iterator Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:51:07
Original
895 people have browsed it

PHP Design Pattern - Iterator Pattern

Iterator pattern: The iterator pattern is a mature pattern for traversing a collection. The key to the iterator pattern is to hand over the task of traversing the collection to an object called an iterator. Its job is to traverse and select objects in the sequence, and The client programmer does not need to know or care about the underlying structure of the collection sequence.

UML class diagram:

Character:

Iterator: Iterator defines the interface for accessing and traversing elements

ConcreteIterator: A concrete iterator implements the iterator interface and tracks the current position when traversing the aggregate

Aggregate: Aggregation definition creates an interface for corresponding iterator objects (optional)

ConcreteAggregate (concrete aggregation): A concrete aggregate implements the interface that creates the corresponding iterator. This operation returns an appropriate instance of ConcreteIterator (optional)

Core code:

<!--?php
/**
 * Created by PhpStorm.
 * User: Jiang
 * Date: 2015/6/8
 * Time: 21:31
 */

//抽象迭代器
abstract class IIterator
{
    public abstract function First();
    public abstract function Next();
    public abstract function IsDone();
    public abstract function CurrentItem();
}

//具体迭代器
class ConcreteIterator extends IIterator
{
    private $aggre;
    private $current = 0;
    public function __construct(array $_aggre)
    {
        $this--->aggre = $_aggre;
    }
    //返回第一个
    public function First()
    {
        return $this->aggre[0];
    }

    //返回下一个
    public function  Next()
    {
        $this->current++;
        if($this->current<count($this->aggre))
        {
            return $this->aggre[$this->current];
        }
        return false;
    }

    //返回是否IsDone
    public function IsDone()
    {
        return $this->current>=count($this->aggre)?true:false;
    }

    //返回当前聚集对象
    public function CurrentItem()
    {
        return $this->aggre[$this->current];
    }
}</count($this->
Copy after login

Call the client test code:

header(Content-Type:text/html;charset=utf-8);
//--------------------------迭代器模式-------------------
require_once ./Iterator/Iterator.php;
$iterator= new ConcreteIterator(array(&#39;周杰伦&#39;,&#39;王菲&#39;,&#39;周润发&#39;));
$item = $iterator->First();
echo $item.
;
while(!$iterator->IsDone())
{
    echo {$iterator->CurrentItem()}:请买票!
;
    $iterator->Next();
}
Copy after login

Usage scenarios:

1. Access the contents of an aggregate object without exposing its internal representation

2. Supports multiple traversals of aggregate objects

3. Provide a unified interface for traversing different aggregate structures

Welcome to follow my video course, the address is as follows, thank you.

PHP object-oriented design patterns


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1015086.htmlTechArticlePHP Design Pattern - Iterator Pattern Iterator Pattern: Iterator pattern is a mature pattern for traversing collections. Iterator The key to the pattern is to hand over the task of traversing the collection to something called an iterator...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template