zy's profile张宇PhotosBlogLists Tools Help

Blog


    May 16

    Iterator模式 2006年5月16日

    <?php
    /**
     * @Iterator功能演示
     * @author   张宇  2006-5-16
     * @version  1.0
     */
     
     interface i_Aggregate  //集合类
     {
         function iterator(); //Iterator`э
       }
       
       interface i_Iterator
       {
         function hasNext(); //boolean类型
         function next(); //Object类型
       }
       
       class Student
     {
        private $name;
      private $sex; //男:1  女:2
     
      public function Student($pName,$pSex) //构造函数
      {
       $this->name = $pName;
       $this->sex = $pSex;
      }
      
      public function getName() //取得姓名
      {
       return $this->name;
      }
      
      public function getSex() //取得性别
      {
       return $this->sex;
      }
     }
     
     class StudentList
     {
      protected $students;  //Student[] students;
      private   $last = 0;
      
      public function StudentList($pStudentCount) //构造函数-指定学生的个数
      {
       $this->students = array($pStudentCount);
      }
     
      public function add(Student $pStudent) //增加一个学生
      {
       $this->students[$this->last] = $pStudent;
       $this->last++;
      }
      
      public function getStudentAt($pIndex)  //返回某个位置的学生
      {
       return $this->students[$pIndex];
      }
      
      public function getLastNum()  //返回学生的数目
      {
       return $this->last;
      }
     }
     
       class MyStudentList extends StudentList implements i_Aggregate
       {
      public function iterator() //Iterator类型
      {
       return new MyStudentListIterator($this);
      }
     }
     
     class MyStudentListIterator implements i_Iterator
     {
      private $myStudentList; //MyStudentList类型
      private $index;
      
      public function MyStudentListIterator(MyStudentList $list)
      {
       $this->myStudentList = $list;
       $this->index = 0;
      }
      
      public function hasNext()
      {
       if($this->myStudentList->getLastNum() > $this->index)
       {
        return true;
       }else {
        return false;
       }
      }
      
      public function next() //Student类型
      {
       $s =  $this->myStudentList->getStudentAt($this->index); //Student类型
       $this->index++;
       return $s;
      }
     }
     
     abstract class Teacher
     {
      protected $sudentList; //StudentList类的实例
      
      public abstract function createStudentList();  //建立一个学生列表
      public abstract function callStudents();  //输出student
     }
     
     class VeteranTeacher extends Teacher
     {
      private $list; //MyStudentList类型
      
      public function createStudentList()
      {
       $this->list = new MyStudentList(6);
       $this->list->add(new Student("张",1));
       $this->list->add(new Student("常",2));
       $this->list->add(new Student("刘",1));
       $this->list->add(new Student("沈",1));
       $this->list->add(new Student("李",2));
       $this->list->add(new Student("蔡",1));
      }
      
      public function callStudents()
      {
       $itr = $this->list->iterator(); //Iterator类型
       while($itr->hasNext())
       {
        echo $itr->next()->getName()."<br>";
       }
      }  
     }
     
     $you = new VeteranTeacher();
     $you->createStudentList();
     $you->callStudents();
    ?>