Wednesday 4 March 2015

Iterator Design Implementation

Iterator Design Pattern


Question:- What is Iterator Design Pattern?

This design pattern falls under Behavioral design pattern. This pattern is used to provide a command and standard way to tranverse through a collection of objects.

As per GOF :-“Provides a way to access the elements of an aggregate object without exposing its underlying represenation.”

An aggregate object is an object that contains other objects for the purpose of grouping those objects as an unit.
Participants of Iterator interface
  • Iterator – defines an interface for accessing and traversing elements.
  • Aggregate – Like a factory method, it defines an interface for creating an iterator object.
  • Concrete Aggregate – This implements the iterator interface and keeps track of the current position in the traversal.
Question:- When to use Iterator design pattern?
This pattern provides a mechanism that allows all elements of a collection to be accessed sequentially, with some operation being performed on each element. With the help of this mechanism we can iterate or looping over an encapsulated group of objects.
  • If we want to access a group of objects without exposing its internal representation.
  • If there are many traversals of objects need to be supported in the collection. It supports multiple, concurrent traversals.

Examples in JDK API
  • implementations of java.util.Iterator and its subclasses.
  • implementations of java.util.Enumeration

Live examples of Iterator design pattern

Iterating over a number of DTH channels or traversing through a song playlist using MP3 player are the live examples of this design pattern.

In the below example, we will see the implementation of Iterator design pattern

DTHChannelMenuEnum.java

package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
public enum DTHChannelMenuEnum {
HINDI, ENGLISH, TELEGU, ALL
}

DTHChannels.java

package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
public class DTHChannels {
private DTHChannelMenuEnum dthChannelMenuEnum;
private double channelFrequency;

public DTHChannels(DTHChannelMenuEnum channelEnum, double chFreq) {
this.dthChannelMenuEnum = channelEnum;
this.channelFrequency = chFreq;
}

public DTHChannelMenuEnum getDthChannelMenuEnum() {
return dthChannelMenuEnum;
}

public double getChannelFrequency() {
return channelFrequency;
}

@Override
public String toString() {
return "DTHChannelMenuEnum=" + this.dthChannelMenuEnum
+ " , ChannelFrequency=" + this.channelFrequency;
}
}


Channels.java


package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
public interface Channels {
 
public void addChannelInFavouriteMenu(DTHChannels dthChannels);
     
   public void removeChannelFromFavouriteMenu(DTHChannels dthChannels);
    
   public DTHMenuChannelIterator iterator(DTHChannelMenuEnum dthChannelMenuEnum);
}



DTHMenuChannelIterator.java


package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
public interface DTHMenuChannelIterator {
 
   public boolean hasNext();
     
   public DTHChannels next();
}



ChannelsImpl.java


package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
import java.util.ArrayList;
import java.util.List;

public class ChannelsImpl implements Channels{
private List<DTHChannels> dthChannelList = new ArrayList<DTHChannels>();

@Override
public void addChannelInFavouriteMenu(DTHChannels dthChannels) {
this.dthChannelList.add(dthChannels);
System.out.println(dthChannels.getDthChannelMenuEnum()+" channel with frequency "+dthChannels.getChannelFrequency()+" is added in the Favourite Menu.");
}

@Override
public void removeChannelFromFavouriteMenu(DTHChannels dthChannels) {
this.dthChannelList.remove(dthChannels);
System.out.println(dthChannels.getDthChannelMenuEnum()+" channel with frequency "+dthChannels.getChannelFrequency()+" is removed from the Favourite Menu.");
}

@Override
public DTHMenuChannelIterator iterator(DTHChannelMenuEnum dthChannelMenuEnum) {
System.out.println("\nTraversing for all DTH channels : ");
return new DTHMenuChannelIteratorImpl(dthChannelMenuEnum, this.dthChannelList);
}
private class DTHMenuChannelIteratorImpl implements DTHMenuChannelIterator
{

private DTHChannelMenuEnum dthChannelMenuEnum;
        private List<DTHChannels> channels;
        private int currentPosition;
        public DTHMenuChannelIteratorImpl(DTHChannelMenuEnum channelEnum,
                List<DTHChannels> dthChannelsArrList) {
            this.dthChannelMenuEnum = channelEnum;
            this.channels = dthChannelsArrList;
        }
        @Override
        public boolean hasNext() {
            while (currentPosition < channels.size()) {
            DTHChannels c = channels.get(currentPosition);
                if (c.getDthChannelMenuEnum().equals(dthChannelMenuEnum) || dthChannelMenuEnum.equals(DTHChannelMenuEnum.ALL)) {
                    return true;
                } else
                    currentPosition++;
            }
            return false;
        }
        @Override
        public DTHChannels next() {
        DTHChannels c = channels.get(currentPosition);
            currentPosition++;
            return c;
        }
    }
}



IteratorDesignPatternDemo.java



package com.gaurav.designpattern.iterator;
/**
 * @author Gaurav
 * 
 */
public class IteratorDesignPatternDemo {
private static Channels retrieveChannels() {
Channels channels = new ChannelsImpl();
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.HINDI, 98.7));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.ENGLISH, 99.8));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.TELEGU, 100.2));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.HINDI, 95.3));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.TELEGU, 98.9));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.ENGLISH, 95.3));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.HINDI, 105.6));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.HINDI, 111.4));
channels.addChannelInFavouriteMenu(new DTHChannels(
DTHChannelMenuEnum.HINDI, 112.9));
return channels;
}
public static void main(String[] args) {
Channels channels = retrieveChannels();
DTHMenuChannelIterator allChannelIterator = channels
.iterator(DTHChannelMenuEnum.ALL);
System.out.println("\nALL available Channels are : \n");
while (allChannelIterator.hasNext()) {
DTHChannels dthChannel = allChannelIterator.next();
System.out.println(dthChannel.toString());
}
System.out.println("\n---------------------------------------------------------------");
DTHMenuChannelIterator hindiChannelIterator = channels
.iterator(DTHChannelMenuEnum.HINDI);
System.out.println("\nHindi available Channels are : \n");
while (hindiChannelIterator.hasNext()) {
DTHChannels dthChannel = hindiChannelIterator.next();
System.out.println(dthChannel.toString());
}
}

}


Result : -

HINDI channel with frequency 98.7 is added in the Favourite Menu.
ENGLISH channel with frequency 99.8 is added in the Favourite Menu.
TELEGU channel with frequency 100.2 is added in the Favourite Menu.
HINDI channel with frequency 95.3 is added in the Favourite Menu.
TELEGU channel with frequency 98.9 is added in the Favourite Menu.
ENGLISH channel with frequency 95.3 is added in the Favourite Menu.
HINDI channel with frequency 105.6 is added in the Favourite Menu.
HINDI channel with frequency 111.4 is added in the Favourite Menu.
HINDI channel with frequency 112.9 is added in the Favourite Menu.

Traversing for all DTH channels : 

ALL available Channels are : 

DTHChannelMenuEnum=HINDI , ChannelFrequency=98.7
DTHChannelMenuEnum=ENGLISH , ChannelFrequency=99.8
DTHChannelMenuEnum=TELEGU , ChannelFrequency=100.2
DTHChannelMenuEnum=HINDI , ChannelFrequency=95.3
DTHChannelMenuEnum=TELEGU , ChannelFrequency=98.9
DTHChannelMenuEnum=ENGLISH , ChannelFrequency=95.3
DTHChannelMenuEnum=HINDI , ChannelFrequency=105.6
DTHChannelMenuEnum=HINDI , ChannelFrequency=111.4
DTHChannelMenuEnum=HINDI , ChannelFrequency=112.9

---------------------------------------------------------------

Traversing for all DTH channels : 

Hindi available Channels are : 

DTHChannelMenuEnum=HINDI , ChannelFrequency=98.7
DTHChannelMenuEnum=HINDI , ChannelFrequency=95.3
DTHChannelMenuEnum=HINDI , ChannelFrequency=105.6
DTHChannelMenuEnum=HINDI , ChannelFrequency=111.4
DTHChannelMenuEnum=HINDI , ChannelFrequency=112.9

No comments:

Post a Comment