Behavioral Pattern - Iterator Pattern (Example) - 1

6 May 2008

The aim is to iterate through a collection of data using a common interface and more importantly, underlying implementation should not be known. We have a behavioral design pattern called ‘Iterator Pattern’ for this. I will introduce you to it in this post.

Iterator should be implemented as an interface. This allows the user to implement it anyway its easier for him/her to return data.
Iwill take an example of daily life and will show how to use iterator pattern in that. You must have used a TV remote control. We use it to change channels. Normal operatios are next channel or back channel. Now interesting thing is that different remote controls have different implementation/technology, but still the interface is the same and that is the reason that you are able to use it easily.

public interface Iterator {
public Channel nextChannel(int currentChannel);
public Channel prevChannel(int currentChannel);
}// End of interface

We declared two methods in the interface that have to be implemented in the class that implements this interface.

continued …

del.icio.us:Behavioral Pattern - Iterator Pattern (Example) - 1  digg:Behavioral Pattern - Iterator Pattern (Example) - 1  spurl:Behavioral Pattern - Iterator Pattern (Example) - 1  wists:Behavioral Pattern - Iterator Pattern (Example) - 1  simpy:Behavioral Pattern - Iterator Pattern (Example) - 1  newsvine:Behavioral Pattern - Iterator Pattern (Example) - 1  blinklist:Behavioral Pattern - Iterator Pattern (Example) - 1  furl:Behavioral Pattern - Iterator Pattern (Example) - 1  reddit:Behavioral Pattern - Iterator Pattern (Example) - 1  fark:Behavioral Pattern - Iterator Pattern (Example) - 1  blogmarks:Behavioral Pattern - Iterator Pattern (Example) - 1  Y!:Behavioral Pattern - Iterator Pattern (Example) - 1  smarking:Behavioral Pattern - Iterator Pattern (Example) - 1  magnolia:Behavioral Pattern - Iterator Pattern (Example) - 1  segnalo:Behavioral Pattern - Iterator Pattern (Example) - 1  gifttagging:Behavioral Pattern - Iterator Pattern (Example) - 1

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Behavioral Pattern - Iterator Pattern (Example) - 1

Leave a Reply