Headfirst设计模式的C++实现——迭代器(Iterator)

iterator.h

 1 #ifndef _ITERATOR_H_
 2 #define _ITERATOR_H_
 3 
 4 #include "menu_item.h"
 5 
 6 class Iterator {
 7 public:
 8     virtual bool has_next() = 0;
 9     virtual MenuItem *next() = 0; 
10 };
11 #endif

menu_item.h

 1 #ifndef _MENU_ITEM_H_
 2 #define _MENU_ITEM_H_
 3 
 4 #include <string>
 5 
 6 class MenuItem {
 7 private:
 8     std::string name;
 9     std::string description;
10     bool vegetarian;
11     double price;
12 public:
13     MenuItem( const std::string &_name,
14               const std::string _description,
15               bool _vegetarian,
16               double _price) :
17         name(_name),
18         description(_description),
19         vegetarian(_vegetarian),
20         price(_price) {}
21     
22     std::string get_name() { return name; }
23     std::string get_description() { return description; }
24     double get_price() { return price; }
25     bool is_vegetarian() { return vegetarian; }
26 };
27 #endif

pancake_house_menu.h

 1 #ifndef _PANCAKE_HOUSE_MENU_H_
 2 #define _PANCAKE_HOUSE_MENU_H_
 3 
 4 #include "menu_item.h"
 5 #include "iterator.h"
 6 
 7 class PancakeHouseMenu {
 8 public:
 9     void add_item(const std::string& name,
10                   const std::string& description,
11                   bool vegetarian,
12                   double price) {
13         if ( item_num < MAX_ITEMS ) {
14            menu_items[item_num++] = new MenuItem( name, description, vegetarian, price ); 
15         }
16     }
17     
18     PancakeHouseMenu() : item_num(0), iterator(*this) {
19         add_item("name_1", "descrption_1", true, 4.5);
20         add_item("name_2", "descrption_2", true, 6.2);
21         add_item("name_3", "descrption_3", false, 3.5);
22     }
23     
24     ~PancakeHouseMenu() {
25         for ( int i = 0; i < item_num; i++ ) {
26             delete menu_items[i];
27         }
28     }
29  
30     Iterator *get_iterator() {
31         return &iterator;
32     }
33     
34 private:
35     class _Iterator : public Iterator {
36     private:
37         PancakeHouseMenu &menu;
38         int pos; 
39     public:
40         _Iterator( PancakeHouseMenu &_menu) : pos(0), menu(_menu) {}
41         bool has_next() { return pos < menu.item_num; }
42         MenuItem *next() {
43             return menu.menu_items[pos++];
44         }
45     } iterator;
46     
47     const static int MAX_ITEMS = 6;
48     MenuItem *menu_items[MAX_ITEMS];
49     int item_num;
50 };
51 #endif

main.cpp

 1 #include "pancake_house_menu.h"
 2 #include <iostream>
 3 
 4 int main() {
 5     PancakeHouseMenu menu;
 6     Iterator *iterator = menu.get_iterator();
 7     while ( iterator->has_next() ) {
 8         MenuItem *menu_item = iterator->next();
 9         std::cout << menu_item->get_name() << " "
10                   << menu_item->get_description() << " "
11                   << menu_item->is_vegetarian() << " "
12                   << menu_item->get_price() << std::endl;
13     }
14 }

 

posted @ 2016-06-03 00:30  Ren.Yu  阅读(454)  评论(0编辑  收藏  举报