Iterator Design Pattern explained with simple example

An iterator is an object which allows a developer to easily traverse through a container class. The Iterator design pattern is relatively simple and easy to implement kind of design pattern in which an iterator is used to traverse through a complex container in order to access the elements of the container. This design pattern tries to decouple container classes from the other modules in order to achieve low coupling.

Before going ahead, have a look at Design pattern simplified version.

Generic model class diagram of Iterator design pattern is as shown in image.

Iterator design pattern class diagram

As shown in the diagram, in this design pattern a client basically uses an iterator object to access aggregator container classes using standard member functions to achieve the desirable behaviour.

Iterator Design Pattern Code Example:

Let’s take an example of typical “Stack” class implementation which can store integer items. Here, we are creating an Iterator class which will allow us to search any item in our stack easily.

#include <iostream>
#include <unistd.h>

#define STACK_SIZE 32
using namespace std;

class StackIterator;

class Stack
{
	private:
		int items[STACK_SIZE];
		int noOfItem;

	public:
		Stack ()
		{
			noOfItem = 0;
		}

		void push (int item)
		{
			items[noOfItem] = item;
			noOfItem++;
		}

		int pop ()
		{
			noOfItem--;
			return items[noOfItem];
		}

		int size ()
		{
			return noOfItem;
		}

		friend class StackIterator;

		StackIterator* createIterator ();
};

class StackIterator
{
	private:
		const Stack* stk;
		int position;

	public:
		StackIterator (const Stack *s)
		{
			stk = s;
			position = 0;
		}

		void begin ()
		{
			position = 0;
		}

		void next ()
		{
			position++;
		}

		bool hasNext ()
		{
			return (position < stk->noOfItem);
		}

		int value ()
		{
			return (stk->items[position]);
		}
};

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

int main ()
{
	Stack st;

	for (int i = 0; i < 10; i++)
	{
		st.push (i*2);
	}

	StackIterator *stItr = st.createIterator ();

	/* search random items */
	while (true)
	{
		cout << "Enter Search number <99 for exit> : " << endl;
		int searchItem;
		cin >> searchItem;

		cout << "Searching for number: " << searchItem << endl;

		if (searchItem == 99)
			break;

		bool found = false;
		for (stItr->begin (); stItr->hasNext (); stItr->next ())
		{
			if (searchItem == stItr->value ())
			{
				found = true;
				break;
			}
		}

		if (found)
		{
			cout << "Hurray, Found the value !!!!" << endl;
		}
		else
		{
			cout << "Oops, didn't found the value" << endl;
		}
	}
}

Output of above example :

Enter Search number <99 for exit> : 
4
Searching for number: 4
Hurray, Found the value !!!!
Enter Search number <99 for exit> : 
7
Searching for number: 7
Oops, didn't found the value
Enter Search number <99 for exit> : 
18
Searching for number: 18
Hurray, Found the value !!!!
Enter Search number <99 for exit> : 
99
Searching for number: 99

Leave a Reply

Your email address will not be published. Required fields are marked *