模式定義
迭代器模式提供一種方法順序訪問一個聚合對象中的各個元素,而又不暴露其內部的表示。
迭代器模式讓我們能游走於聚合內的每一個元素,而又不暴露其內部的表示。把游走的任務放在迭代器上,而不是聚合上。這樣簡化了聚合的接口和實現,也讓責任各得其所。
模式結構:
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICBJdGVyYXRvcqO6tfy0+sb3tqjS5bfDzsq6zbHpwPrUqsvYtcS907/aPC9wPgo8cD4gICAgICAgIENvbmNyZXRlSXRlcmF0b3I6vt/M5bX8tPrG98q1z9a1/LT6xve907/ao7u21LjDvtu6z7HpwPrKsbj619m1scewzrvWwzwvcD4KPHA+ICAgICAgICBBZ2dyZWdhdGU6vtu6z7ao0uW0tL2oz+DTprXEtfy0+sb3ttTP873Tv9o8L3A+CjxwPiAgICAgICAgQ29uY3JldGVBZ2dyZWdhdGU6vt/M5b7bus/Ktc/WtLS9qM/g06a1/LT6xve1xL3Tv9qjrLjDstnX97e1u9hDb25jcmV0ZUl0ZXJhdG9ytcTSu7j2ysq1sbXEyrXA/aGjPC9wPgo8aDE+vtnA/aO6PC9oMT4KPHA+ICAgICAgICC85bH9zt26zc7nss216rrPsqK689Do0qq2qNbG0ru33dDCtcSyzbWlo6y1q9PJ09q85bH9zt21xNStssu1pcrH08PBtLHtyrXP1qOstvjO57LNtePUrbLLtaXKx9PDyv3X6cq1z9Yoy/vDx7XEtqjS5cjnz8LL+cq+KaOsy/nS1LTy06HQwrLNtaW1xMqxuvLQ6NKqt9ax8NGtu7ex6cD61K2yzbWl1tC1xLLLtaXP7qGjPC9wPgo8cCBhbGlnbj0="left">
//菜單項類
class MenuItem
{
public:
MenuItem(){}
MenuItem(string na, string descrip, double pric)
{
name = na;
description = descrip;
price = pric;
}
string getName()
{
return name;
}
string getDescription()
{
return description;
}
double getPrice()
{
return price;
}
private:
string name;
string description;
double price;
};
//煎餅屋餐單類
class PancakeHouseMenu
{
public:
PancakeHouseMenu()
{
addItem("K&B'S Breakfase","pacakes with eggs",2.99);
addItem("Buleberry Breakfase","pacakes with buleberries",3.99);
}
void addItem(string na, string descrip, double ric)
{
MenuItem menuItem(na,descrip,ric);
menuItems.push_back(menuItem);
}
list