Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169631
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:56:24

迭代器模式 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。




  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>

  5. using namespace std;


  6. struct MenuItem {
  7.     string name;
  8.     string description;
  9.     bool vegetarian;
  10.     double price;
  11.  
  12.     MenuItem(string name,
  13.              string description,
  14.              bool vegetarian,
  15.              double price)
  16.     {
  17.         this->name = name;
  18.         this->description = description;
  19.         this->vegetarian = vegetarian;
  20.         this->price = price;
  21.     }
  22.   
  23.     string getName() {
  24.         return name;
  25.     }
  26.   
  27.     string getDescription() {
  28.         return description;
  29.     }
  30.   
  31.     double getPrice() {
  32.         return price;
  33.     }
  34.   
  35.     bool isVegetarian() {
  36.         return vegetarian;
  37.     }
  38.     string toString() {
  39.         ostringstream oss;
  40.         oss << name << ", $" << price << "\n " << description;
  41.         return oss.str();
  42.     }
  43. };

  44. struct Iterator {
  45.     virtual bool hasNext() = 0;
  46.     virtual MenuItem *next() = 0;
  47. };

  48. struct Menu {
  49.     virtual Iterator *createIterator() = 0;
  50. };


  51. struct DinerMenuIterator : public Iterator {
  52. public:
  53.     DinerMenuIterator(MenuItem **items) {
  54.         position = 0;
  55.         this->items = items;
  56.     }
  57.  
  58.     MenuItem *next() {
  59.         MenuItem *menuItem = items[position];
  60.         position = position + 1;
  61.         return menuItem;
  62.     }
  63.  
  64.     bool hasNext() {
  65.         if (position >= 6) {
  66.             return false;
  67.         } else {
  68.             return true;
  69.         }
  70.     }
  71. private:
  72.     MenuItem **items;
  73.     int position;
  74. };


  75. struct DinerMenu : public Menu {
  76. public:
  77.     DinerMenu() {
  78.         numberOfItems = 0;
  79.         addItem("Vegetarian BLT",
  80.             "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);
  81.         addItem("BLT",
  82.             "Bacon with lettuce & tomato on whole wheat", false, 2.99);
  83.         addItem("Soup of the day",
  84.             "Soup of the day, with a side of potato salad", false, 3.29);
  85.         addItem("Hotdog",
  86.             "A hot dog, with saurkraut, relish, onions, topped with cheese",
  87.             false, 3.05);
  88.         addItem("Steamed Veggies and Brown Rice",
  89.             "Steamed vegetables over brown rice", true, 3.99);
  90.         addItem("Pasta",
  91.             "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
  92.             true, 3.89);
  93.     }
  94.   
  95.     addItem(string name, string description,
  96.      bool vegetarian, double price)
  97.     {
  98.         MenuItem *menuItem = new MenuItem(name, description, vegetarian, price);
  99.         if (numberOfItems >= MAX_ITEMS) {
  100.             cout << "Sorry, menu is full! Can't add item to menu" << endl;
  101.         } else {
  102.             menuItems[numberOfItems] = menuItem;
  103.             numberOfItems = numberOfItems + 1;
  104.         }
  105.     }
  106.  
  107.     MenuItem *getMenuItems() {
  108.         return *menuItems;
  109.     }
  110.       
  111.     virtual Iterator *createIterator() {
  112.         return new DinerMenuIterator(menuItems);
  113.     }
  114. private:
  115.     static const int MAX_ITEMS = 6;
  116.     int numberOfItems;
  117.     MenuItem *menuItems[MAX_ITEMS];
  118. };


  119. struct PancakeHouseMenuIterator : public Iterator {
  120. public:
  121.     PancakeHouseMenuIterator(vector<MenuItem *> items) {
  122.         position = 0;
  123.         this->items = items;
  124.     }
  125.  
  126.     MenuItem *next() {
  127.         MenuItem *menuItem = items[position];
  128.         position = position + 1;
  129.         return menuItem;
  130.     }
  131.  
  132.     bool hasNext() {
  133.         if (position >= items.size()) {
  134.             return false;
  135.         } else {
  136.             return true;
  137.         }
  138.     }
  139. private:
  140.     vector<MenuItem *> items;
  141.     int position;
  142. };


  143. struct PancakeHouseMenu : public Menu {
  144. public:
  145.     PancakeHouseMenu() {
  146.         addItem("K&B's Pancake Breakfast",
  147.             "Pancakes with scrambled eggs, and toast",
  148.             true,
  149.             2.99);
  150.  
  151.         addItem("Regular Pancake Breakfast",
  152.             "Pancakes with fried eggs, sausage",
  153.             false,
  154.             2.99);
  155.  
  156.         addItem("Blueberry Pancakes",
  157.             "Pancakes made with fresh blueberries",
  158.             true,
  159.             3.49);
  160.  
  161.         addItem("Waffles",
  162.             "Waffles, with your choice of blueberries or strawberries",
  163.             true,
  164.             3.59);
  165.     }

  166.     addItem(string name, string description,
  167.      bool vegetarian, double price)
  168.     {
  169.         MenuItem *menuItem = new MenuItem(name, description, vegetarian, price);
  170.         menuItems.push_back(menuItem);
  171.     }
  172.  
  173.     vector<MenuItem *> getMenuItems() {
  174.         return menuItems;
  175.     }
  176.   
  177.     virtual Iterator *createIterator() {
  178.         return new PancakeHouseMenuIterator(menuItems);
  179.     }
  180.   
  181.     string toString() {
  182.         return "Objectville Pancake House Menu";
  183.     }
  184. private:
  185.     vector<MenuItem *> menuItems;
  186. };

  187. struct Waitress {
  188. public:
  189.     Waitress(Menu *pancakeHouseMenu, Menu *dinerMenu) {
  190.         this->pancakeHouseMenu = pancakeHouseMenu;
  191.         this->dinerMenu = dinerMenu;
  192.     }

  193.     printMenu() {
  194.         Iterator *pancakeIterator = pancakeHouseMenu->createIterator();
  195.         Iterator *dinerIterator = dinerMenu->createIterator();
  196.         cout << "MENU\n----\nBREAKFAST" << endl;
  197.         printMenu(pancakeIterator);
  198.         cout << "\nLUNCH" << endl;
  199.         printMenu(dinerIterator);
  200.     }

  201.     printMenu(Iterator *iterator) {
  202.         while (iterator->hasNext()) {
  203.             MenuItem *menuItem = (MenuItem *)iterator->next();
  204.             cout << menuItem->getName() << ", ";
  205.             cout << menuItem->getPrice() << " -- ";
  206.             cout << menuItem->getDescription() << endl;
  207.         }
  208.     }
  209. private:
  210.     Menu *pancakeHouseMenu;
  211.     Menu *dinerMenu;
  212. };

  213. int main(int argc, char **argv)
  214. {
  215.     PancakeHouseMenu *pancakeHouseMenu = new PancakeHouseMenu();
  216.     DinerMenu *dinerMenu = new DinerMenu();
  217.     Waitress *waitress = new Waitress(pancakeHouseMenu, dinerMenu);

  218.     waitress->printMenu();
  219. }

阅读(750) | 评论(0) | 转发(0) |
0

上一篇:模板方法模式

下一篇:状态模式

给主人留下些什么吧!~~