意图
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
动机
一个聚合对象,如list,应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构,此外,针对不同的需要,可能要以不同的方式遍历这个列表。但是即使可以预见到所需的那些遍历操作,你可能也不希望列表的接口中充斥着各种不同遍历的操作。有时还可能需要在同一个列表上同时进行多个遍历。
适用性
- 访问一个聚合对象的内容而无需暴露它的内部表示。
- 支持对聚合对象的多种遍历。
- 为遍历不同的聚合结构提供一个统一的接口。
类图
例子场景
对象村餐厅和对象村煎饼屋要合并了,我们需要遍历他们的菜单,但是对象村煎饼屋使用List来存储菜单,对象村餐厅使用数组来存储,实现一个易扩展的框架来遍历。
实现
Menu.h
#pragma once
class MenuIterator;
class Menu
{
public:
Menu();
virtual MenuIterator* createIterator()=0;
};
Menu.cpp
#include "Menu.h"
#include "Iterator.h"
Menu::Menu()
{
}
MenuItem.h
#pragma once
#include <string>
class MenuItem
{
public:
MenuItem();
MenuItem(const std::string &name, const std::string &description, const double &price);
std::string getName() { return m_name; }
std::string getDescription() { return m_description; }
double getPrice() { return m_price; }
private:
std::string m_name;
std::string m_description;
double m_price;
};
MenuItem.cpp
#include "MenuItem.h"
MenuItem::MenuItem()
{
}
MenuItem::MenuItem(const std::string& name, const std::string& description, const double& price)
: m_name(name)
, m_description(description)
, m_price(price)
{
}
MenuIterator.h
#pragma once
class MenuIterator
{
public:
MenuIterator();
virtual bool hasNext() = 0;
virtual void* next() = 0;
};
MenuIterator.cpp
#include "Iterator.h"
MenuIterator::MenuIterator()
{
}
DinerMenuIterator.h
#pragma once
#include "Iterator.h"
class MenuItem;
class DinerMenuIterator : public MenuIterator
{
public:
DinerMenuIterator(MenuItem *pItems);
bool hasNext() override;
void* next() override;
private:
MenuItem* m_items;
int m_nPosition;
static const int MaxSize;
};
DinerMenuIterator.cpp
#include "DinerMenuIterator.h"
#include "MenuItem.h"
const int DinerMenuIterator::MaxSize = 2;
DinerMenuIterator::DinerMenuIterator(MenuItem* pItems)
: m_items(pItems)
, m_nPosition(0)
{
}
bool DinerMenuIterator::hasNext()
{
if (m_nPosition >= MaxSize)
{
return false;
}
return true;
}
void* DinerMenuIterator::next()
{
MenuItem* pMenuItem = &m_items[m_nPosition];
m_nPosition++;
return pMenuItem;
}
PancakeHouseMenuIterator.h
#pragma once
#include "Iterator.h"
#include "MenuItem.h"
#include <vector>
class PancakeHouseMenuIterator : public MenuIterator
{
public:
PancakeHouseMenuIterator(const std::vector<MenuItem *> &items);
bool hasNext() override;
void* next() override;
private:
std::vector<MenuItem*> m_items;
std::vector<MenuItem*>::iterator m_iterator;
};
PancakeHouseMenuIterator.cpp
#include "PancakeHouseMenuIterator.h"
PancakeHouseMenuIterator::PancakeHouseMenuIterator(const std::vector<MenuItem*>& items)
: m_items(items)
{
m_iterator = m_items.begin();
}
bool PancakeHouseMenuIterator::hasNext()
{
return m_iterator != m_items.end();
}
void* PancakeHouseMenuIterator::next()
{
return *m_iterator++;
}
DinerMenu.h
#pragma once
#include "Menu.h"
#include "MenuItem.h"
class MenuItem;
class DinerMenu : public Menu
{
public:
DinerMenu();
MenuIterator* createIterator() override;
private:
MenuItem m_items[2];
};
DinerMenu.cpp
#include "DinerMenu.h"
#include "DinerMenuIterator.h"
#include "MenuItem.h"
DinerMenu::DinerMenu()
{
m_items[0] = MenuItem("egg", "egg", 1.0);
m_items[1] = MenuItem("apple", "apple", 2.0);
}
MenuIterator* DinerMenu::createIterator()
{
return new DinerMenuIterator(m_items);
}
PancakeHouseMenu.h
#pragma once
#include "Menu.h"
#include <vector>
class MenuItem;
class PancakeHouseMenu : public Menu
{
public:
PancakeHouseMenu();
MenuIterator* createIterator() override;
private:
std::vector<MenuItem*> m_items;
};
PancakeHouseMenu.cpp
#include "PancakeHouseMenu.h"
#include "PancakeHouseMenuIterator.h"
PancakeHouseMenu::PancakeHouseMenu()
{
m_items.push_back(new MenuItem("banane", "banane", 3.0));
m_items.push_back(new MenuItem("bear", "bear", 5.5));
}
MenuIterator* PancakeHouseMenu::createIterator()
{
return new PancakeHouseMenuIterator(m_items);
}
main.cpp
#include "Waitress.h"
#include "PancakeHouseMenu.h"
#include "DinerMenu.h"
#include <memory>
using namespace std;
int main()
{
shared_ptr<DinerMenu> pDinerMenu = make_shared<DinerMenu>();
shared_ptr<PancakeHouseMenu> pPancakeHouseMenu = make_shared<PancakeHouseMenu>();
Waitress waitress(pDinerMenu.get(), pPancakeHouseMenu.get());
waitress.printMenu();
return 0;
}