Behavioral Pattern - Iterator Pattern (Example) - 2
6 May 2008The 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.
Related Posts:
- Behavioral Pattern - Iterator Pattern (Example) - 1
- The Strategy Design Pattern in Java(I)
- The Observer Design Pattern(I)
- 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) - 2