//今天一口气把这一章前半部分的iterator例子的所有代码写完,涉及到了不少指针的内容,竟然一次性编译通过。。。。
//Iterator与Menu之间应该不是has a的关系,先这样着吧。
#ifndef __MENUITEM_H__
#define __MENUITEM_H__
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class MenuItem
{
private:
string name;
string description;
bool vegetarian;
double price;
public:
MenuItem(){}
MenuItem(const string &na, const string des, bool vege, double pr)
{
name = na;
description = des;
vegetarian = vege;
price = pr;
}
virtual ~MenuItem(){}
string getName(){ return name; }
string getDescription(){ return description; }
double getPrice(){ return price; }
bool isVegetarian(){ return vegetarian; }
};
#endif
#ifndef __ITERATOR_H__
#define __ITERATOR_H__
#include "MenuItem.h"
class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){}
virtual bool hasNext() = 0;
virtual MenuItem* next() = 0;
};
class DinerIterator : public Iterator
{
private:
MenuItem ** menuItem;
int position;
int length;
public:
DinerIterator(MenuItem **m, int le) :menuItem(m), length(le), position(0){}
virtual ~DinerIterator(){}
virtual MenuItem* next()
{
MenuItem *p = menuItem[position];
position++;
return p;
}
virtual bool hasNext()
{
if (position >= length || menuItem[position] == NULL)
{
return false;
}
return true;
}
};
class PancakeIterator : public Iterator
{
private:
vector<MenuItem*> menuItem;
int position;
int length;
public:
PancakeIterator(vector<MenuItem*> &m) :position(0), length(m.size())
{
menuItem = m;
}
virtual ~PancakeIterator(){}
virtual MenuItem* next()
{
MenuItem *p = menuItem[position];
position++;
return p;
}
virtual bool hasNext()
{
if (position >= length || menuItem[position] == NULL)
{
return false;
}
return true;
}
};
#endif
#ifndef __MENU_H__
#define __MENU_H__
#include "Iterator.h"
class Menu
{
public:
Menu(){}
virtual ~Menu(){}
virtual Iterator * CreateIterator()= 0;
};
class PackageHouseMenu :public Menu
{
private:
vector<MenuItem*> menuItem;
public:
PackageHouseMenu()
{
addItem("PancakeMunu: BlueBerry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);
addItem("PancakeMunu: Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);
}
~PackageHouseMenu()
{
if (!menuItem.empty())
{
for (size_t i = 0; i < menuItem.size(); i++)
{
delete menuItem[i];
}
menuItem.clear();
}
}
void addItem(const string na, const string des, bool vege, double pr)
{
MenuItem *t = new MenuItem(na, des, vege, pr);
menuItem.push_back(t);
}
vector<MenuItem*> getMenuItem(){ return menuItem; }
Iterator *CreateIterator()
{
Iterator *iter = new PancakeIterator(menuItem);
return iter;
}
};
class DinerMenu :public Menu
{
private:
static const int MAXITEMS = 6;
int numberOfItems;
MenuItem * menuItem[MAXITEMS];
public:
DinerMenu()
{
numberOfItems = 0;
for (int i = 0; i < MAXITEMS; i++)
{
menuItem[i] = NULL;
}
addItem("DinerMenu: Hot dog", "a hot dog,with sauerkraut, relish, onions", false, 3.05);
addItem("DinerMenu: BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99);
}
~DinerMenu()
{
if (numberOfItems != 0)
{
for (int i = 0; i < numberOfItems + 1; i++)
{
delete menuItem[i];
}
}
}
void addItem(const string na, const string des, bool vege, double pr)
{
if (numberOfItems > MAXITEMS)
{
cout << "Error, Can't Add More!" << endl;
}
else
{
MenuItem *p = new MenuItem(na, des, vege, pr);
menuItem[numberOfItems] = p;
numberOfItems++;
}
}
MenuItem** getMenuItems()
{
return menuItem;
}
Iterator *CreateIterator()
{
Iterator *iter = new DinerIterator(menuItem, numberOfItems + 1);
return iter;
}
};
#endif
#ifndef __WAITRESS_H__
#define __WAITRESS_H__
#include "Menu.h"
class Waitress
{
private:
Menu *panckage;
Menu *diner;
public:
Waitress(Menu *p, Menu *d)
{
panckage = p;
diner = d;
}
~Waitress()
{
}
void printMenu(Iterator *it)
{
while (it->hasNext())
{
MenuItem *p = it->next();
cout << p->getName() << ", " << p->getDescription() << "," << p->getPrice() << endl;
}
}
void printMenu()
{
Iterator* pan = panckage->CreateIterator();
Iterator* din = diner->CreateIterator();
printMenu(pan);
printMenu(din);
}
};
#endif
#include <iostream>
#include "Waitress.h"
using namespace std;
int main()
{
PackageHouseMenu *p = new PackageHouseMenu();
DinerMenu *d = new DinerMenu();
Waitress *w = new Waitress(p, d);
w->printMenu();
delete w;
delete p;
delete d;
return 0;
}