Wednesday, August 22, 2007

Iterator pattern

hi,
long time since my last post :( sorry.

today i will be discussing about a new pattern

scenario :

i want to list all the cars and two wheelers in my locality.i have two classes "cars" and "bikes". but instead of cosing for these classes, i have asked two of my friends to code. they have done a commendable job,everything implemented and works properly. but now, the problem is one of the classes return the list in the form of an arraylist and the other in the form of array.
example :
public arraylist GetCars()
and
public object[] GetBikes()

my friends have given me their code. i have to use it. i will have a difficulty using them cause i will have to use arraylist in one instance and object[] in another. For arraylist, i can use foreach, but for array, i will have to use for loop.
if both of them were somewhat similar, i could have used a generic function to get the objects from the list. But lets get back to reality, both of them are different. how do i manage? do i change the arraylist in one of the classes to array type so that i can use the generic method? or do i change the array to arraylist?
let us think - is it really necessary to change the data structure used to store the data? is it not a complicated change? i really do not know the logic which my friends have used.
is there any other possibility?

yes, there is!!!
the Iterator pattern is the answer.

i will have an interface called iIterator which looks like this

public interface iIterator
{
bool MoveNext();
object getCurrent();

}

and the classes car and twowheeler impements this interface.
MoveNext() will return true/false based on whether the array / arraylist has an object in the next position and moves the cursor to the next position.
getCurrent() will return an object from the current cursor location.

what i did was instead of returning the arraylist/array from the class, i return each individual object and then move the cursor to the next location.

this will be clear if we look into the example for the class which has arraylist.

public class Cars : iIterator
{
private int Cursor = 0;
Arraylist Cars = new Arraylist();

//-----------------------------------------
//the code for filling up the arraylist goes here
//lets not get into it as we are aware of how this is done
------------------------------------------//
public object getCurrent()
{
return arraylist[Cursor];
}

public bool MoveNext()
{
Cursor+=1;
}

}


the same will be done for any other data structure used for storage like array,list etc.

hope this article helps you.

happy coding !!!!

2 comments:

SANDIP ROY said...

Good post nandan. i will see it more. But i think of another problem... ur solution is u give them the interface before they developed the respective solution.

i would like to integrate their solution after one returns arraylist and another as object array. Because they are not ur friends :), but external parties :(

Howzzat!
Sandip

nandan said...

sandip,
the use of iterator pattern is that it forces the classes which implement the interface to use iterator model rather than individual data dtructure.
if i create a class which has to implement this interface, then its freedon to use any data structure remains, but the class does not have any liberty in exposing the data-structure and its members.