码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-06-03 22:51:04      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

iterator.h

1 #ifndef _ITERATOR_H_
2 #define _ITERATOR_H_
3 
4 class Iterator {
5 public:
6     virtual bool has_next() = 0;
7     virtual void *next() = 0; 
8 };
9 #endif

menu.h

1 #ifndef _MENU_H_
2 #define _MENU_H_
3 
4 class Menu {
5 public:
6     virtual Iterator *get_iterator() = 0;
7     virtual ~Menu() {};
8 };
9 #endif

diner_menu.h

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

main.cpp

 1 #include "pancake_house_menu.h"
 2 #include "diner_menu.h"
 3 #include <iostream>
 4 
 5 int main() {
 6     Menu * menus[] = { new PancakeHouseMenu, new DinerMenu };    
 7     for ( int i = 0; i < sizeof(menus)/sizeof(menus[0]); i++ ) {
 8         Iterator *iterator = menus[i]->get_iterator();
 9         while ( iterator->has_next() ) {
10             MenuItem *menu_item = (MenuItem *)iterator->next();
11             std::cout << menu_item->get_name() << " "
12                       << menu_item->get_description() << " "
13                       << menu_item->is_vegetarian() << " "
14                       << menu_item->get_price() << std::endl;
15         } 
16         delete menus[i];
17     }
18 }

 

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

标签:

原文地址:http://www.cnblogs.com/ren-yu/p/5557655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!