#include using namespace std; #define MAX_ITEMS 20 typedef const char * String; class Iterator; class Aggregate{ public: virtual Iterator *CreateIterator()=0; }; class ConcreteAggregate:public Aggregate{ String items[MAX_ITEMS]; int noi; public: friend class ConcreteIterator; ConcreteAggregate():noi(0){ for(int i=0;i=noi)) throw "Out of bound!"; return items[i]; } }; class Iterator{ public: virtual String First()=0; virtual String Next()=0; virtual bool IsDone()=0; virtual String CurrentItem()=0; }; class ConcreteIterator: public Iterator{ ConcreteAggregate* aggregate; int current; public: ConcreteIterator(ConcreteAggregate* aggregate): aggregate(aggregate),current(0){ } String First(){ if (current != 0) throw "Invalid First"; return (*aggregate)[current++]; } String Next(){ return (currentCount())?(*aggregate)[current++]:0; } String CurrentItem(){ return (*aggregate)[current]; } bool IsDone(){ return (current==aggregate->Count()); } }; Iterator *ConcreteAggregate::CreateIterator(){ return new ConcreteIterator(this); } main(){ ConcreteAggregate a; a.Add("Item A"); a.Add("Item B"); a.Add("Item C"); a.Add("Item D"); Iterator *i = new ConcreteIterator(&a); cout<<"Iterating over collection:"<First(); cout<<"isDone="<<(i->IsDone()?"True":"False")<Next(); } cout<<"isDone="<<(i->IsDone()?"True":"False")<