Iterator - shoonie/StudyingDesignPattern GitHub Wiki

Intent

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. ex) STL iterator

Diagram

disgram

Consequences

  1. It supports variations in the traversal of an aggregate.
  2. Iterators simplify the Aggregate interface.
  3. More than one traversal can be pending on an aggregate.

Implementation

  1. Who controls the iteration? A fundamental issue is deciding which party controls the iteration, the iterator or the client that uses the iterator. When the client controls the iteration, the iterator is called an external iterator, and when the iterator controls it, the iterator is an internal iterator.
  2. Who defines the traversal algorithm?
  3. How robust is the iterator? A robust iterator ensures that insertions and removals won't interfere with traversal, and it does it without copying the aggregate.
  4. Additional Iterator operations. The minimal interface to iterator consist of the operations First, Next, IsDone, and CurrentItem.
  5. Using polymorphic iterator in C++
  6. Iterator may have privileged access.
  7. Iterator for composite.
  8. Null iterators.

Sample Code

//https://sourcemaking.com/design_patterns/iterator/cpp/1

#include <iostream>
using namespace std;

class Stack
{
    int items[10];
    int sp;
  public:
    friend class StackIter;
    Stack()
    {
        sp =  - 1;
    }
    void push(int in)
    {
        items[++sp] = in;
    }
    int pop()
    {
        return items[sp--];
    }
    bool isEmpty()
    {
        return (sp ==  - 1);
    }
    StackIter *createIterator()const; // 2. Add a createIterator() member
};

class StackIter
{
    // 1. Design an "iterator" class
    const Stack *stk;
    int index;
  public:
    StackIter(const Stack *s)
    {
        stk = s;
    }
    void first()
    {
        index = 0;
    }
    void next()
    {
        index++;
    }
    bool isDone()
    {
        return index == stk->sp + 1;
    }
    int currentItem()
    {
        return stk->items[index];
    }
};

StackIter *Stack::createIterator()const
{
  return new StackIter(this);
}

bool operator == (const Stack &l, const Stack &r)
{
  // 3. Clients ask the container object to create an iterator object
  StackIter *itl = l.createIterator();
  StackIter *itr = r.createIterator();
  // 4. Clients use the first(), isDone(), next(), and currentItem() protocol
  for (itl->first(), itr->first(); !itl->isDone(); itl->next(), itr->next())
    if (itl->currentItem() != itr->currentItem())
      break;
  bool ans = itl->isDone() && itr->isDone();
  delete itl;
  delete itr;
  return ans;
}

int main()
{
  Stack s1;
  for (int i = 1; i < 5; i++)
    s1.push(i);
  Stack s2(s1), s3(s1), s4(s1), s5(s1);
  s3.pop();
  s5.pop();
  s4.push(2);
  s5.push(9);
  cout << "1 == 2 is " << (s1 == s2) << endl;
  cout << "1 == 3 is " << (s1 == s3) << endl;
  cout << "1 == 4 is " << (s1 == s4) << endl;
  cout << "1 == 5 is " << (s1 == s5) << endl;
}
⚠️ **GitHub.com Fallback** ⚠️