Difference between revisions of "Iterator Pattern"

From Logic Wiki
Jump to: navigation, search
(Iterator Interface)
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
 
This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
  
[[File:Iterator.png]]
+
[[File:Iterator.jpg]]
 +
== Implementation ==
 +
=== Iterator Interface ===
 +
public interface Iterator {
 +
  public boolean hasNext();
 +
  public Object next();
 +
}
 +
it can be
 +
 
 +
public interface Iterator {
 +
  public boolean hasNext();
 +
  public void next();
 +
  public Object Current();
 +
}
 +
 
 +
=== Container Interface ===
 +
public interface Container {
 +
  public Iterator getIterator();
 +
}
 +
=== Concrete class implementing the Container interface ===
 +
<pre>
 +
public class NameRepository implements Container {
 +
  public String names[] = {"Robert" , "John" ,"Julie" , "Lora"};
 +
 
 +
  @Override
 +
  public Iterator getIterator() {
 +
      return new NameIterator();
 +
  }
 +
 
 +
  private class NameIterator implements Iterator {
 +
 
 +
      int index;
 +
 
 +
      @Override
 +
      public boolean hasNext() {
 +
     
 +
        if(index < names.length){
 +
            return true;
 +
        }
 +
        return false;
 +
      }
 +
 
 +
      @Override
 +
      public Object next() {
 +
     
 +
        if(this.hasNext()){
 +
            return names[index++];
 +
        }
 +
        return null;
 +
      }
 +
  }
 +
}
 +
</pre>
 +
 
 +
=== Implementation Detail ===
 +
<pre>
 +
public class IteratorPatternDemo {
 +
 +
  public static void main(String[] args) {
 +
      NameRepository namesRepository = new NameRepository();
 +
 
 +
      for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){
 +
        String name = (String)iter.next();
 +
        System.out.println("Name : " + name);
 +
      }
 +
  }
 +
}
 +
</pre>

Latest revision as of 15:02, 20 March 2019


Video

https://www.youtube.com/watch?v=uNTNEfwYXhI

Definition

This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.

Iterator.jpg

Implementation

Iterator Interface

public interface Iterator {
  public boolean hasNext();
  public Object next();
}

it can be

public interface Iterator {
  public boolean hasNext();
  public void next();
  public Object Current();
}

Container Interface

public interface Container {
  public Iterator getIterator();
}

Concrete class implementing the Container interface

public class NameRepository implements Container {
   public String names[] = {"Robert" , "John" ,"Julie" , "Lora"};

   @Override
   public Iterator getIterator() {
      return new NameIterator();
   }

   private class NameIterator implements Iterator {

      int index;

      @Override
      public boolean hasNext() {
      
         if(index < names.length){
            return true;
         }
         return false;
      }

      @Override
      public Object next() {
      
         if(this.hasNext()){
            return names[index++];
         }
         return null;
      }		
   }
}

Implementation Detail

public class IteratorPatternDemo {
	
   public static void main(String[] args) {
      NameRepository namesRepository = new NameRepository();

      for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){
         String name = (String)iter.next();
         System.out.println("Name : " + name);
      } 	
   }
}