- #include <iostream>
- #include <string>
- using namespace std;
-
- // prototype
- template <class T> class Iterator;
- template <class T> class ConcreteIterator;
-
- // The 'Aggregate' abstract class
- template <class T>
- class Aggregate {
- public:
- virtual ~Aggregate() { }
- virtual Iterator<T>* CreateIterator() = 0;
- };
-
- // The 'ConcreteAggregate' class
- template <class T>
- class ConcreteAggregate : public Aggregate<T> {
- public:
- // constructor
- ConcreteAggregate(int size) {
- _items = new T[size];
- _max_size = size;
- _count = 0;
- }
- // destructor
- virtual ~ConcreteAggregate() {
- delete [] _items;
- }
- // CreateIterator
- Iterator<T>* CreateIterator() {
- return new ConcreteIterator<T>(this);
- }
- // Count
- int Count() {
- return _count;
- }
- // Get
- T* Get(int index) {
- return &_items[index];
- }
- // Set
- void Set(const T& value) {
- if (_count < _max_size) {
- _items[_count++] = value;
- }
- }
- private:
- T *_items;
- int _max_size;
- int _count;
- };
-
- // The 'Iterator' abstract class
- template <class T>
- class Iterator {
- public:
- virtual ~Iterator() { }
- virtual T* First() = 0;
- virtual T* Next() = 0;
- virtual T* CurrentItem() = 0;
- virtual bool IsDone() = 0;
- };
-
- // The 'ConcreteIterator' class
- template <class T>
- class ConcreteIterator : public Iterator<T> {
- public:
- // constructor
- ConcreteIterator(ConcreteAggregate<T> *aggregate) {
- _aggregate = aggregate;
- _current = 0;
- }
- // First
- T* First() {
- return _aggregate->Get(0);
- }
- // Next
- T* Next() {
- T *ret = NULL;
- if (_current < _aggregate->Count() - 1) {
- ret = _aggregate->Get(++_current);
- }
- return ret;
- }
- // CurrentItem
- T* CurrentItem() {
- return _aggregate->Get(_current);
- }
- // IsDone
- bool IsDone() {
- return (_current >= _aggregate->Count());
- }
- private:
- ConcreteAggregate<T> *_aggregate;
- int _current;
- };
-
- // client
- int main() {
- ConcreteAggregate<string> *a = new ConcreteAggregate<string>(4);
- a->Set("Item A");
- a->Set("Item B");
- a->Set("Item C");
- a->Set("Item D");
-
- ConcreteIterator<string> *i = new ConcreteIterator<string>(a);
- // Iterator<string> *i = a->CreateIterator(); // 上と同じ
-
- string *item = i->First();
- while (item != NULL) {
- cout << *item << endl;
- item = i->Next();
- }
-
- delete a;
- delete i;
- return 0;
- }
-
- #include <iostream>
- #include <string>
- using namespace std;
-
- // prototype
- template <class T> class IAbstractIterator;
- template <class T> class Iterator;
-
- // A collection item
- class Item {
- public:
- // constructor
- Item(const string &name) {
- _name = name;
- }
- // Get
- string GetName() {
- return _name;
- }
- private:
- string _name;
- };
-
- // The 'Aggregate' interface class
- template <class T>
- class IAbstractCollection {
- public:
- virtual ~IAbstractCollection() { };
- virtual Iterator<T>* CreateIterator() = 0;
- };
-
- // The 'ConcreteAggregate' class
- template <class T>
- class Collection : public IAbstractCollection<T> {
- public:
- // constructor
- Collection(int size) {
- _items = new T[size];
- _max_size = size;
- _count = 0;
- }
- // destructor
- virtual ~Collection() {
- delete [] _items;
- }
- // CreateIterator
- Iterator<T>* CreateIterator() {
- return new Iterator<T>(this);
- }
- // Count
- int Count() {
- return _count;
- }
- // Get
- T* Get(int index) {
- return &_items[index];
- }
- // Set
- void Set(const T& value) {
- if (_count < _max_size) {
- _items[_count++] = value;
- }
- }
- private:
- T *_items;
- int _max_size;
- int _count;
- };
-
- // The 'Iterator' interface class
- template <class T>
- class IAbstractIterator {
- public:
- virtual ~IAbstractIterator() { };
- virtual T* First() = 0;
- virtual T* Next() = 0;
- virtual T* CurrentItem() = 0;
- virtual bool IsDone() = 0;
- };
-
- // The 'ConcreteIterator' class
- template <class T>
- class Iterator : public IAbstractIterator<T> {
- public:
- // constructor
- Iterator(Collection<T> *collection) {
- _collection = collection;
- _current = 0;
- _step = 1;
- }
- // First
- T* First() {
- _current = 0;
- return _collection->Get(0);
- }
- // Next
- T* Next() {
- _current += _step;
- if (!IsDone()) {
- return _collection->Get(_current);
- } else {
- return NULL;
- }
- }
- // GetStep
- int GetStep() {
- return _step;
- }
- // SetStep
- void SetStep(int step) {
- _step = step;
- }
- // CurrentItem
- T* CurrentItem() {
- return _collection->Get(_current);
- }
- // IsDone
- bool IsDone() {
- return (_current >= _collection->Count());
- }
- private:
- Collection<T> *_collection;
- int _current;
- int _step;
- };
-
- // client
- int main() {
- Collection<Item*> *collection = new Collection<Item*>(9);
- collection->Set(new Item("Item 0"));
- collection->Set(new Item("Item 1"));
- collection->Set(new Item("Item 2"));
- collection->Set(new Item("Item 3"));
- collection->Set(new Item("Item 4"));
- collection->Set(new Item("Item 5"));
- collection->Set(new Item("Item 6"));
- collection->Set(new Item("Item 7"));
- collection->Set(new Item("Item 8"));
-
- // Create iterator
- Iterator<Item*> *iterator = new Iterator<Item*>(collection);
-
- // Skip every other item
- iterator->SetStep(2);
-
- for (Item **item = iterator->First();
- !iterator->IsDone();
- item = iterator->Next())
- {
- cout << (*item)->GetName() << endl;
- }
-
- delete collection;
- delete iterator;
- return 0;
- }
-
- #include <iostream>
- #include <string>
- using namespace std;
-
- // prototype
- class BookIterator;
-
- // 集約されるオブジェクトクラス
- class Book {
- public:
- Book(const string &name) {
- m_name = name;
- }
- string GetName() {
- return m_name;
- }
- private:
- string m_name;
- };
-
- // Aggregate: 集約クラス
- class BookAggregate {
- public:
- // constructor
- BookAggregate() {
- m_plist = NULL;
- m_list_size = 0;
- }
- // destructor
- ~BookAggregate() {
- BookList *p, *next;
- for (p = m_plist; p != NULL; p = next) {
- delete p->book;
- next = p->next;
- delete p;
- }
- }
- // Add
- void Add(Book *book) {
- // リスト最後尾へ追加
- BookList *p, *p_bfr = NULL;
- int size = 1;
- for (p = m_plist; p != NULL; p_bfr = p, p = p->next, ++size);
- p = new BookList;
- p->book = book;
- if (m_plist == NULL) {
- // 初回は先頭を確保
- m_plist = p;
- }
- if (p_bfr != NULL) {
- // 初回以外は前の要素へ今作った要素へのポインタを確保
- p_bfr->next = p;
- }
- // リストサイズ更新
- m_list_size = size;
- }
- // Get
- Book* Get(int index) {
- BookList *p, *p_bfr = NULL;
- int i;
- if (index < 0 || index >= m_list_size) {
- return NULL;
- }
- for (i = 0, p = m_plist; p != NULL; ++i, p = p->next) {
- if (i == index) {
- return p->book;
- }
- }
- return NULL;
- }
- // Size
- int Size() {
- return m_list_size;
- }
- // CreateIterator
- BookIterator* CreateIterator();
- private:
- // リスト構造体定義
- struct BookList {
- Book *book;
- BookList *next;
- BookList() {
- book = NULL;
- next = NULL;
- }
- }; // struct BookList
- BookList *m_plist; // list
- int m_list_size; // size
- };
-
- // Iterator: 反復子クラス
- class BookIterator {
- public:
- // constructor
- BookIterator(BookAggregate *aggregate) {
- m_aggregate = aggregate;
- m_current = 0;
- }
- // First
- void First() {
- m_current = 0;
- }
- // Next
- void Next() {
- ++m_current;
- }
- // IsDone
- bool IsDone() {
- return (m_current >= m_aggregate->Size());
- }
- // CurrentItem
- Book* CurrentItem() {
- return m_aggregate->Get(m_current);
- }
- // Destroy
- void Destroy() {
- delete this;
- }
- private:
- BookAggregate *m_aggregate;
- int m_current;
- };
-
- // BookAggregate::CreateIterator
- BookIterator* BookAggregate::CreateIterator() {
- return new BookIterator(this);
- }
-
- // client
- int main() {
- BookAggregate *list = new BookAggregate();
- // 登録
- list->Add(new Book("book 1"));
- list->Add(new Book("book 2"));
- list->Add(new Book("book 3"));
-
- // iterator で参照
- BookIterator *it = list->CreateIterator();
- for (it->First(); !it->IsDone(); it->Next()) {
- cout << it->CurrentItem()->GetName() << endl;
- }
-
- // 終了処理
- it->Destroy();
- delete list;
- return 0;
- }
-
book 1
book 2
book 3
- #include <iostream>
- #include <string>
-
- using namespace std;
-
- // 生徒クラス
- class Student {
- public:
- Student(const string& name, int sex) {
- m_name = name;
- m_sex = sex;
- }
- string getName() {
- return m_name;
- }
- int getSex() {
- return m_sex;
- }
- private:
- string m_name;
- int m_sex;
- };
-
-
- // 生徒リスト
- class StudentList {
- public:
- StudentList(int count) {
- m_students = new Student*[count];
- m_last = 0;
- }
- ~StudentList() {
- for (int i=0; i<getLastNum(); ++i) {
- delete m_students[i];
- }
- delete [] m_students;
- }
- void add(Student* student) {
- m_students[m_last++] = student;
- }
- Student* getStudentAt(int index) {
- return m_students[index];
- }
- int getLastNum() {
- return m_last;
- }
- protected:
- Student **m_students;
- private:
- int m_last;
- };
-
-
- // 新しい生徒リスト
- #include <vector>
- class NewStudentList {
- public:
- virtual ~NewStudentList() {
- for (int i=0; i<m_students.size(); ++i) {
- delete m_students.at(i);
- }
- }
- void add(Student* student) {
- m_students.push_back(student);
- }
- Student* getStudentAt(int index) {
- return m_students.at(index);
- }
- int getListSize() { // インターフェイスが変わった
- return m_students.size();
- }
- protected:
- vector<Student*> m_students;
- };
-
-
- // イテレータ
- class Iterator {
- public:
- virtual bool hasNext() = 0;
- virtual void* next() = 0;
- };
-
-
- // アグリゲート
- class Aggregate {
- public:
- virtual Iterator* iterator() = 0;
- };
-
-
- // プロトタイプ(前方)宣言
- // 継承していることを前方宣言で表すことはできない
- //class MyStudentListIterator;
-
-
- #if 0
- // イテレータするための生徒リスト
- class MyStudentList
- : public StudentList, public Aggregate
- {
- public:
- MyStudentList(int count) : StudentList(count) { }
- Iterator* iterator(); // MyStudentListIterator を使うので、宣言後に。
- };
- #else
- // イテレータするための新しい生徒リスト
- class MyStudentList
- : public NewStudentList, public Aggregate
- {
- public:
- Iterator* iterator();
- };
- #endif
-
-
- // イテレータの実装
- class MyStudentListIterator : public Iterator {
- public:
- MyStudentListIterator(MyStudentList* list) {
- m_StudentList = list;
- m_index = 0;
- }
- bool hasNext() {
- #if 0
- if (m_StudentList->getLastNum() > m_index) {
- return true;
- } else {
- return false;
- }
- #else
- // 新しい生徒リストはインターフェイスが変わった
- if (m_StudentList->getListSize() > m_index) {
- return true;
- } else {
- return false;
- }
- #endif
- }
- void* next() {
- return (void*)m_StudentList->getStudentAt(m_index++);
- }
- private:
- MyStudentList *m_StudentList;
- int m_index;
- };
-
-
- // MyStudentListIterator 宣言後に書く
- Iterator* MyStudentList::iterator()
- {
- // ポインタ/参照ではないので、前方宣言ではコンパイルエラー
- return new MyStudentListIterator(this);
- }
-
-
- int main(int argc, char **argv) {
- // リスト登録
- #if 0
- MyStudentList *list = new MyStudentList(2);
- #else
- // 新しい生徒リストに変更
- MyStudentList *list = new MyStudentList;
- #endif
- list->add(new Student("たろう", 1));
- list->add(new Student("はなこ", 2));
-
- // イテレータを使って表示
- Iterator* itr = list->iterator();
- while (itr->hasNext()) {
- cout << ((Student*)itr->next())->getName() << endl;
- }
- delete list;
-
- return 0;
- }
-