提供一种方法顺序访问一个集合对象中的各种元素,而又不暴露该对象的内部表示。

foreach 的底层就是迭代器。很多编程语言都已经将其作为一个基础类库实现出来了,所以也就有了这个模式目前学习意义大于实际意义的说法。

在 php 中,内部已提供 Iterator 接口,可以直接使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Bookshelf implements \Countable, \Iterator
{
protected array $books = [];
protected int $current = 0;


public function addBook(Book $book)
{
$this->books[] = $book;
}

public function current()
{
return $this->books[$this->current];
}

public function next()
{
return $this->current++;
}

public function key(): int
{
return $this->current;
}

public function valid(): bool
{
return isset($this->books[$this->current]);
}

public function rewind()
{
$this->current = 0;
}

public function count(): int
{
return count($this->books);
}
}

class Book
{
protected string $author;
protected string $title;

public function __construct(string $author, string $title)
{
$this->author = $author;
$this->title = $title;
}

public function getAuthor(): string
{
return $this->author;
}

public function getTitle(): string
{
return $this->title;
}

public function getAuthorAndTitle(): string
{
return $this->getAuthor() . '-' . $this->getTitle();
}
}

$bookA = new Book('wu', 'php');
$bookB = new Book('wu', 'redis');

$bookshelf = new Bookshelf();
$bookshelf->addBook($bookA);
$bookshelf->addBook($bookB);

foreach ($bookshelf as $book) {
echo $book->getAuthorAndTitle(), PHP_EOL;
}

使用起来还是比较简单的,至于如何实现就不写了。