JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Iterator design pattern:
Purpose
  • Provide a way to access the elements of an aggregate object sequentially without exposing
    its underlying representation
Application

  • To access an aggregate object's contents without exposing its internal representation.
  • Tto support multiple traversals of aggregate objects.
  • Tto provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).

Examples of 
Iterator


  • On early television sets, a dial was used to change channels. When channel
    surfing, the viewer was required to move the dial through each channel
    position, regardless of whether or not that channel had reception.
  •  On modern television sets, a next and previous button are used. When the viewer selects
    the "next" button, the next tuned channel will be displayed. 
  • Consider watching television in a hotel room in a strange city. 
  • When surfing through channels, the channel number is not important, but the programming is. 
  • If the programming on one channel is not of interest, the viewer can request the
    next channel, without knowing its number.

  • The Iterator provides ways to access elements of an aggregate object sequentially
    without exposing the underlying structure of the object. Files are aggregate objects.
  • In office settings where access to files is made through administrative or secretarial
    staff, the Iterator pattern is demonstrated with the secretary acting as the Iterator.
  • Several television comedy skits have been developed around the premise of an
    executive trying to understand the secretary's filing system. To the executive, the
    filing system is confusing and illogical, but the secretary is able to access files
    quickly and efficiently.
Example:-

EmployeeIterable.java
package com.adapter;

import java.util.Iterator;

import com.model.Employee;

public class EmployeeIterable
 
implements Iterable<Employee>{
  Iterator<Employee> itr;
  
  public EmployeeIterable(Iterator<Employee> itr) {
    super();
    this.itr = itr;
  }

  @Override
  public Iterator<Employee> iterator() {
    // TODO Auto-generated method stub
    return itr;
  }

}

EmployeeIterator.java
package com.adapter;

import java.util.Enumeration;
import java.util.Iterator;

import com.model.Employee;

public class EmployeeIterator implements Iterator<Employee>{
  Enumeration<Employee> e1;
  public EmployeeIterator(Enumeration<Employee> e2) {
    // TODO Auto-generated constructor stub
    e1=e2;
  }
  @Override
  public boolean hasNext() {
    // TODO Auto-generated method stub
    return e1.hasMoreElements();
  }

  @Override
  public Employee next() {
    // TODO Auto-generated method stub
    return e1.nextElement();
  }

  @Override
  public void remove() {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
  }

}

Employee.java
package com.model;

public class Employee {
  private int empid;
  private String ename;
  private String designation;
  private double salary;
  public int getEmpid() {
    return empid;
  }
  public void setEmpid(int empid) {
    this.empid = empid;
  }
  public String getEname() {
    return ename;
  }
  public void setEname(String ename) {
    this.ename = ename;
  }
  public String getDesignation() {
    return designation;
  }
  public void setDesignation(String designation) {
    this.designation = designation;
  }
  public double getSalary() {
    return salary;
  }
  public void setSalary(double salary) {
    this.salary = salary;
  }
  @Override
  public String toString() {
    // TODO Auto-generated method stub
    return empid+" "+ename+" "+designation+" "+salary;
  }
  public Employee(int empid, String ename,
String designation, 
double salary) {
    super();
    this.empid = empid;
    this.ename = ename;
    this.designation = designation;
    this.salary = salary;
  }
  
}

EmployeeList.java
package com.model;

import java.util.Enumeration;

public class EmployeeList
 
implements Enumeration<Employee>{
  Employee employees[];
  int top=-1;
  int iterator=-1;
  public EmployeeList() {
    // TODO Auto-generated constructor stub
    employees=new Employee[30];
  }
  public void addEmploee(Employee e1)
  {
    employees[++top]=e1;
  }
  @Override
  public boolean hasMoreElements() {
    // TODO Auto-generated method stub
    return (iterator<top);
    
  }
  @Override
  public Employee nextElement() {
    // TODO Auto-generated method stub
    return employees[++iterator];
  }
  
                     
}

Test.java
package com.test;

import java.util.Enumeration;

import com.model.Employee;
import com.model.EmployeeList;

public class Test {
  public static void main(String []arg){
    EmployeeList list=new EmployeeList();
    list.addEmploee(new Employee(1,"abcd1","abcd",3000));
    list.addEmploee(new Employee(2,"abcd2","abcd",3000));
    list.addEmploee(new Employee(3,"abcd3","abcd",3000));
    list.addEmploee(new Employee(4,"abcd4","abcd",3000));
    list.addEmploee(new Employee(5,"abcd5","abcd",3000));
    list.addEmploee(new Employee(6,"abcd6","abcd",3000));
    list.addEmploee(new Employee(7,"abcd7","abcd",3000));
    list.addEmploee(new Employee(8,"abcd8","abcd",3000));
    Enumeration<Employee> e1=list;
    while(e1.hasMoreElements()){
      System.out.println(e1.nextElement());
    }
  }
}

Output:-



Test2.java
package com.test;

import java.util.Iterator;

import com.adapter.EmployeeIterable;
import com.adapter.EmployeeIterator;
import com.model.Employee;
import com.model.EmployeeList;

public class Test2 {

  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    EmployeeList list=new EmployeeList();
    list.addEmploee(new Employee(1,"abcd1","abcd",3000));
    list.addEmploee(new Employee(2,"abcd2","abcd",3000));
    list.addEmploee(new Employee(3,"abcd3","abcd",3000));
    list.addEmploee(new Employee(4,"abcd4","abcd",3000));
    list.addEmploee(new Employee(5,"abcd5","abcd",3000));
    list.addEmploee(new Employee(6,"abcd6","abcd",3000));
    list.addEmploee(new Employee(7,"abcd7","abcd",3000));
    list.addEmploee(new Employee(8,"abcd8","abcd",3000));
    Iterator< Employee> itr=new EmployeeIterator(list);
    
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
  }

}

Output:-




Test3.java
package com.test;

import com.adapter.EmployeeIterable;
import com.adapter.EmployeeIterator;
import com.model.Employee;
import com.model.EmployeeList;

public class Test3 {

  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    EmployeeList list=new EmployeeList();
    list.addEmploee(new Employee(1,"abcd1","abcd",3000));
    list.addEmploee(new Employee(2,"abcd2","abcd",3000));
    list.addEmploee(new Employee(3,"abcd3","abcd",3000));
    list.addEmploee(new Employee(4,"abcd4","abcd",3000));
    list.addEmploee(new Employee(5,"abcd5","abcd",3000));
    list.addEmploee(new Employee(6,"abcd6","abcd",3000));
    list.addEmploee(new Employee(7,"abcd7","abcd",3000));
    list.addEmploee(new Employee(8,"abcd8","abcd",3000));
    EmployeeIterator itr=new EmployeeIterator(list);
    for(Employee e1:new EmployeeIterable(itr))
      System.out.println(e1);
  }

}

Output:-



Download Zip code