迭代器模式

2017-05-30  本文已影响6人  散装咖啡
/**
* iterator模式
*
* 提供一个方法顺序访问一聚合对象中的各个元素,而又不暴露对象的内部表
示
*/
    interface Interator
    {
        public function next();
        public function first();
        public function current();
        public function isDone();
    }

    class SomeInterator implements Interator
    {
        private $_arr = array();
        
        public function __construct($arr)
        {
            $this->_arr =$arr;
        }
        
        public function first()
        {
            return $this->_arr[0];
        }
        
        public function current()
        {
            return current($this->_arr);
        }
        
        public function next()
        {
            return next($this->_arr);
        }
        
        public function isDone()
        {
            
        }
    }

$objSomeInterator = new SomeInterator(array(1,2,3,4,6,7));

echo $objSomeInterator->first(),"<br/>";
echo $objSomeInterator->next(),"<br/>";
echo $objSomeInterator->current(),"<br/>";
echo $objSomeInterator->current(),"<br/>";
echo $objSomeInterator->next(),"<br/>";
echo $objSomeInterator->current(),"<br/>";

参考文章 https://wenku.baidu.com/view/b4c78b4702768e9951e738fb.html

上一篇下一篇

猜你喜欢

热点阅读