Behavioral Pattern - Iterator Pattern (Example) - 2

6 May 2008

The defined channel iterator will be common for all the remote controls. So we know which method will do our required task, but the method has to be implemented by the class implementing this interface.

Continuing with the example, I present the ChannelSurfer class that implements the Iterator interface and thus overrides prevChannel and nextChannel methods.

public ChannelSurfer implements Iterator {
 
public Channel nextChannel (int currentChannel) { 
Channel channel = new Channel(currentChannel+1);
return channel;
}	 
 
public Channel prevChannel (int currentChannel) {
Channel channel = new Channel(currentChannel-1);
return channel;
}	 
}// End of class

The following class used the whole thing to make use of it.

public class RemoteControl {
private ChannelSurfer surfer;
private Settings settings; 
public RemoteControl() {
 
surfer = new ChannelSurfer();
settings = new Settings();
}	 
 
public getProgram(ChannelSurfer surfer) {
return new Program(surfer.nextChannel());
}	 	 
}// End of class

I hope this helps.

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

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) - 2

Leave a Reply