<Head First 设计模式>:迭代器模式1:Iterator

//今天一口气把这一章前半部分的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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值