迭代器模式

printMenu();

addItem('煎饼1', '这是煎饼1', true, 5.5);
        $this->addItem('煎饼2', '这是煎饼2', true, 6.5);
        $this->addItem('煎饼3', '这是煎饼3', true, 7.5);
    }

    protected function addItem($name, $description, $vegetarian, $price) {
        $menu_item = new MenuItem($name, $description, $vegetarian, $price);
        $this->_menu_items[] = $menu_item;
    }

    public function getIterator() {
        yield new MenuItem('煎饼1', '这是煎饼1', true, 5.5);
        yield new MenuItem('煎饼2', '这是煎饼2', true, 6.5);
        yield new MenuItem('煎饼3', '这是煎饼3', true, 7.5);
    }

    public function createIterator() {
        return new PancakeMenuIterator($this->_menu_items);
    }
}
addItem('宫保鸡丁', '宫保鸡丁描述', true, 12);
        $this->addItem('鱼香肉丝', '鱼香肉丝描述', true, 16);
        $this->addItem('火爆腰花', '火爆腰花描述', true, 35);
    }

    protected function addItem($name, $description, $vegetarian, $price) {
        $menu_item = new MenuItem($name, $description, $vegetarian, $price);
        $this->_menu_items[] = $menu_item;
    }

    public function createIterator() {
        return new DinerMenuIterator($this->_menu_items);
    }

}
_menu_items = $menu_items;
    }

    public function hasNext() {
        return isset($this->_menu_items[$this->_position]);
    }

    public function next() {
        $current_item = $this->_menu_items[$this->_position] ?? null;

        $this->_position ++;

        return $current_item;
    }

}
_menu_items = $menu_items;
    }

    public function hasNext() {
        return isset($this->_menu_items[$this->_position]);
    }

    public function next() {
        $current_item = $this->_menu_items[$this->_position] ?? null;

        $this->_position ++;

        return $current_item;
    }

}
_name = $name;
        $this->_description = $description;
        $this->_vegetarian = $vegetarian;
        $this->_price = $price;
    }

    /**
     * @return mixed
     */
    public function getName() {

        return $this->_name;
    }

    /**
     * @return mixed
     */
    public function getDescription() {

        return $this->_description;
    }

    /**
     * @return mixed
     */
    public function getPrice() {

        return $this->_price;
    }

    /**
     * @return mixed
     */
    public function getVegetarian() {

        return $this->_vegetarian;
    }

}

_pancake_house_menu = $pancake_house_menu;
        $this->_diner_menu = $diner_menu;

    }

    public function printMenu() {
        echo 'Breakfast Menu:', PHP_EOL;
        echo '=================', PHP_EOL;

        $pancake_iterator = $this->_pancake_house_menu->createIterator();

        $this->printMenuByIterator($pancake_iterator);

        echo PHP_EOL;
        echo 'Lunch Menu:', PHP_EOL;
        echo '=================', PHP_EOL;

        $diner_iterator = $this->_diner_menu->createIterator();

        $this->printMenuByIterator($diner_iterator);
    }

    protected function printMenuByIterator(MenuIterator $iterator) {
        while($iterator->hasNext()) {
            $menu_item = $iterator->next();
            echo 'name: ', $menu_item->getName(), PHP_EOL;
            echo 'price: ', $menu_item->getPrice(), PHP_EOL;
            echo 'description: ', $menu_item->getDescription(), PHP_EOL;
            echo 'vegetarian: ', $menu_item->getVegetarian(), PHP_EOL;
        }
    }

}
此条目发表在 DesignPattern 分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>