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

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

时间:2016-06-03 01:16:52      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

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

标签:

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

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