Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170259
  • 博文数量: 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 23:04:55




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

  5. using namespace std;


  6. struct MenuComponent {
  7.     virtual add(MenuComponent *menuComponent) {}
  8.     virtual remove(MenuComponent *menuComponent) {}
  9.     virtual MenuComponent *getChild(int i) {return NULL;}
  10.     virtual string getName() {return "";}
  11.     virtual string getDescription() {return "";}
  12.     virtual double getPrice() {return 0.0;}
  13.     virtual bool isVegetarian() {return false;}
  14.     virtual print() {}
  15. };

  16. struct MenuItem : public MenuComponent {
  17.     string name;
  18.     string description;
  19.     bool vegetarian;
  20.     double price;
  21.  
  22.     MenuItem(string name,
  23.              string description,
  24.              bool vegetarian,
  25.              double price)
  26.     {
  27.         this->name = name;
  28.         this->description = description;
  29.         this->vegetarian = vegetarian;
  30.         this->price = price;
  31.     }
  32.   
  33.     virtual string getName() {
  34.         return name;
  35.     }
  36.   
  37.     virtual string getDescription() {
  38.         return description;
  39.     }
  40.   
  41.     virtual double getPrice() {
  42.         return price;
  43.     }
  44.   
  45.     virtual bool isVegetarian() {
  46.         return vegetarian;
  47.     }
  48.     virtual print() {
  49.         cout << " " << getName();
  50.         if (isVegetarian()) {
  51.             cout << "(V)";
  52.         }
  53.         cout << ", " << getPrice();
  54.         cout << " -- " << getDescription() << endl;
  55.     }
  56. };

  57. struct Menu : public MenuComponent {
  58.     vector<MenuComponent *> menuComponents;
  59.     string name;
  60.     string description;

  61.     Menu(string name, string description) {
  62.         this->name = name;
  63.         this->description = description;
  64.     }
  65.     virtual add(MenuComponent *menuComponent) {
  66.         menuComponents.push_back(menuComponent);
  67.     }
  68.     virtual remove(MenuComponent menuComponent) {
  69.         //menuComponents.remove(menuComponent);
  70.     }
  71.     virtual MenuComponent *getChild(int i) {
  72.         return menuComponents[i];
  73.     }
  74.     virtual string getName() {
  75.         return name;
  76.     }
  77.     virtual string getDescription() {
  78.         return description;
  79.     }
  80.     virtual print() {
  81.         cout << "\n" << getName();
  82.         cout << ", " << getDescription() << endl;
  83.         cout << "-------------------------" << endl;

  84.         vector<MenuComponent *>::iterator it;
  85.         for (it = menuComponents.begin(); it != menuComponents.end(); ++it) {
  86.             (*it)->print();
  87.         }
  88.     }
  89. };

  90. struct Waitress {
  91. public:
  92.     Waitress(MenuComponent *allMenus) {
  93.         this->allMenus = allMenus;
  94.     }

  95.     printMenu() {
  96.         allMenus->print();
  97.     }
  98. private:
  99.     MenuComponent *allMenus;
  100. };

  101. int main(int argc, char **argv)
  102. {
  103.     MenuComponent *pancakeHouseMenu =
  104.         new Menu("PANCAKE HOUSE MENU", "Breakfast");
  105.     MenuComponent *dinerMenu =
  106.         new Menu("DINER MENU", "Lunch");
  107.     MenuComponent *cafeMenu =
  108.         new Menu("CAFE MENU", "Dinner");
  109.     MenuComponent *dessertMenu =
  110.         new Menu("DESSERT MENU", "Dessert of course!");

  111.     MenuComponent *allMenus = new Menu("ALL MENUS", "All menus combined");

  112.     allMenus->add(pancakeHouseMenu);
  113.     allMenus->add(dinerMenu);
  114.     allMenus->add(cafeMenu);

  115.     pancakeHouseMenu->add(new MenuItem(
  116.         "K&B's Pancake Breakfast",
  117.         "Pancakes with scrambled eggs, and toast",
  118.         true,
  119.         2.99));
  120.  
  121.     pancakeHouseMenu->add(new MenuItem(
  122.         "Regular Pancake Breakfast",
  123.         "Pancakes with fried eggs, sausage",
  124.         false,
  125.         2.99));
  126.  
  127.     pancakeHouseMenu->add(new MenuItem(
  128.         "Blueberry Pancakes",
  129.         "Pancakes made with fresh blueberries",
  130.         true,
  131.         3.49));
  132.  
  133.     pancakeHouseMenu->add(new MenuItem(
  134.         "Waffles",
  135.         "Waffles, with your choice of blueberries or strawberries",
  136.         true,
  137.         3.59));

  138.     dinerMenu->add(new MenuItem(
  139.         "Vegetarian BLT",
  140.         "(Fakin') Bacon with lettuce & tomato on whole wheat",
  141.         true,
  142.         2.99));
  143.     dinerMenu->add(new MenuItem(
  144.         "BLT",
  145.         "Bacon with lettuce & tomato on whole wheat",
  146.         false,
  147.         2.99));
  148.     dinerMenu->add(new MenuItem(
  149.         "Soup of the day",
  150.         "Soup of the day, with a side of potato salad",
  151.         false,
  152.         3.29));
  153.     dinerMenu->add(new MenuItem(
  154.         "Hotdog",
  155.         "A hot dog, with saurkraut, relish, onions, topped with cheese",
  156.         false,
  157.         3.05));
  158.     dinerMenu->add(new MenuItem(
  159.         "Steamed Veggies and Brown Rice",
  160.         "Steamed vegetables over brown rice",
  161.         true,
  162.         3.99));
  163.     dinerMenu->add(new MenuItem(
  164.         "Pasta",
  165.         "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
  166.         true,
  167.         3.89));

  168.     dinerMenu->add(dessertMenu);

  169.     dessertMenu->add(new MenuItem(
  170.         "Apple Pie",
  171.         "Apple pie with a flakey crust, topped with vanilla ice cream",
  172.         true,
  173.         1.59));

  174.     Waitress *waitress = new Waitress(allMenus);

  175.     waitress->printMenu();
  176. }

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

上一篇:代理模式

下一篇:桥接模式

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