[php]php设计模式 Interator (迭代器模式)

1 <?php
2 /**
3 * 迭代器模式
4 *
5 * 提供一个方法顺序访问一聚合对象中的各个元素,而又不暴露对象的内部表示
6 */
7 interface Interator
8 {
9 publicfunctionnext();
10 publicfunction first();
11 publicfunctioncurrent();
12 publicfunction isDone();
13 }
14
15 class SomeInterator implements Interator
16 {
17 private$_arr=array();
18
19 publicfunction __construct($arr)
20 {
21 $this->_arr =$arr;
22 }
23
24 publicfunction first()
25 {
26 return$this->_arr[0];
27 }
28
29 publicfunctioncurrent()
30 {
31 returncurrent($this->_arr);
32 }
33
34 publicfunctionnext()
35 {
36 returnnext($this->_arr);
37 }
38
39 publicfunction isDone()
40 {
41 }
42 }
43
44 $objSomeInterator=new SomeInterator(array(1,2,3,4,6,7));
45 echo$objSomeInterator->first(),"<br/>";
46 echo$objSomeInterator->next(),"<br/>";
47 echo$objSomeInterator->current(),"<br/>";
48 echo$objSomeInterator->current(),"<br/>";
49 echo$objSomeInterator->next(),"<br/>";
50 echo$objSomeInterator->current(),"<br/>";

posted on 2011-06-29 21:59  bluefrog  阅读(1759)  评论(0编辑  收藏  举报