Behavioral Pattern - Iterator Pattern (Example) - 1
6 May 2008The 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 …
Related Posts:
- The Strategy Design Pattern in Java(I)
- The Observer Design Pattern(I)
- Behavioral Pattern - Iterator Pattern (Example) - 2
- Singleton - Creational Design Pattern (I)
- Adapter design pattern
- The Strategy Design Pattern in Java(II)
- The Facade Design Pattern (I)
- Matching Patters using Regex
- The Facade Design Pattern (II)
- Singleton - Creational Design Pattern (II)
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