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/>";

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2021-11-03
  • 2023-04-06
  • 2021-11-13
  • 2022-01-02
  • 2021-09-04
猜你喜欢
  • 2022-01-11
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
  • 2023-04-06
相关资源
相似解决方案