Design Pattern > Behavioral Design Pattern > Iterator Design Pattern
The Intent of Iterator Design pattern is to trasverse through the elements of an aggregate object by passing it to Iterator.
Java Iterator is a nice implementation of iterator design pattern allowing us to traverse through a collection in a sequential way.
Benefits of Using Iterator Pattern :
Abstracting the details of the collection away from the task of sequentially traversing through the objects allows us to process any kind of collection without the tight coupling of having to know anything about the objects. This leads to cleaner and more readable code.
Iterator pattern is not all about traversing the collection of objects sequentially, you can modify/create new iterator to perform parallel iteration based on filtering as well.
Key Terms :
Iterator -
ConcreteIterator -
Aggregate -
ConcreteAggregete -
Client -
Java Implementation :
/**
* Iterator interface
*/
package com.kmingle.iterator;
public interface Iterator {
Employee nextEmployee();
boolean isLastEmployee();
Employee currentEmployee();
}
Contractor Employee : c1
Contractor Employee : c2
Permanent Employee : p1
Permanent Employee : p2
Permanent Employee : p3
Permanent Employee : p4
The Intent of Iterator Design pattern is to trasverse through the elements of an aggregate object by passing it to Iterator.
Java Iterator is a nice implementation of iterator design pattern allowing us to traverse through a collection in a sequential way.
Benefits of Using Iterator Pattern :
Abstracting the details of the collection away from the task of sequentially traversing through the objects allows us to process any kind of collection without the tight coupling of having to know anything about the objects. This leads to cleaner and more readable code.
Iterator pattern is not all about traversing the collection of objects sequentially, you can modify/create new iterator to perform parallel iteration based on filtering as well.
Key Terms :
Iterator -
- An interface , declares methods for accessing and traversing elements
ConcreteIterator -
- Implements Iterator
- Stores collection of elements ( passed in constructor )
- Keeps track of current position in the traversal of Aggregate
- Returns the next element in traversal
Aggregate -
- An Interface, declares methods to create Iterator object and to get collection of elements
ConcreteAggregete -
- Implements Aggregate
- Initialize collection of elements ( through constructor )
- Return Iterator object
- Return collection of elements
Client -
- Class to create Iterator object(s) by calling Agreegate's method to access elements.
Java Implementation :
/**
* Iterator interface
*/
package com.kmingle.iterator;
public interface Iterator {
Employee nextEmployee();
boolean isLastEmployee();
Employee currentEmployee();
}
/**
* Concrete Iterator
*/
package com.kmingle.iterator;
import java.util.List;
public class ContractEmployeeIterator implements Iterator {
public List employeeList;
private int position;
public ContractEmployeeIterator(List empList){
this.employeeList = empList;
}
@Override
public Employee nextEmployee() {
Employee emp = null;
for(; position < employeeList.size(); position++){
if("contractor".equals(((Employee) employeeList.get(position)).getEmpType())){
emp = (Employee) employeeList.get(position);
position++;
break;
}
}
return emp;
}
@Override
public boolean isLastEmployee() {
for(int i=position; i< employeeList.size();i++){
if("contractor".equals(((Employee) employeeList.get(i)).getEmpType())){
return false;
}
}
return true;
}
@Override
public Employee currentEmployee() {
if(position < employeeList.size()){
return (Employee) employeeList.get(position);
}
return null;
}
}
/**
* Concrete Iterator
*/
package com.kmingle.iterator;
import java.util.List;
public class PermanentEmployeeIterator implements Iterator {
public List employeeList;
private int position;
public PermanentEmployeeIterator(List empList){
this.employeeList = empList;
}
@Override
public Employee nextEmployee() {
Employee emp = null;
for(; position < employeeList.size(); position++){
if("permanent".equals(((Employee) employeeList.get(position)).getEmpType())){
emp = (Employee) employeeList.get(position);
position++;
break;
}
}
return emp;
}
@Override
public boolean isLastEmployee() {
for(int i=position; i< employeeList.size();i++){
if("permanent".equals(((Employee) employeeList.get(i)).getEmpType())){
return false;
}
}
return true;
}
@Override
public Employee currentEmployee() {
if(position < employeeList.size()){
return (Employee) employeeList.get(position);
}
return null;
}
}
/**
* AggregateInterface
*/
package com.kmingle.iterator;
import java.util.List;
public interface Company {
List getEmployee();
void addEmployee(Employee emp);
void removeEmployee(Employee emp);
Iterator createIterator(String iteratorType);
}
/**
* Concrete Aggregate
*/
package com.kmingle.iterator;
import java.util.ArrayList;
import java.util.List;
public class CompanyImpl implements Company {
List empList;
public CompanyImpl(){
empList = new ArrayList();
}
@Override
public List getEmployee() {
return empList;
}
@Override
public void addEmployee(Employee emp) {
empList.add(emp);
}
@Override
public void removeEmployee(Employee emp) {
empList.remove(emp);
}
@Override
public Iterator createIterator(String iteratorType) {
if("contractor".equals(iteratorType)){
return new ContractEmployeeIterator(empList);
}
else {
return new PermanentEmployeeIterator(empList);
}
}
}
/**
* Client Class
*/
package com.kmingle.iterator;
public class IteratorDemo {
public static void main(String[] args) {
CompanyImpl company = new CompanyImpl();
company.addEmployee(new Employee("p1", "permanent"));
company.addEmployee(new Employee("c1", "contractor"));
company.addEmployee(new Employee("p2", "permanent"));
company.addEmployee(new Employee("p3", "permanent"));
company.addEmployee(new Employee("c2", "contractor"));
company.addEmployee(new Employee("p4", "permanent"));
Iterator contractIterator = company.createIterator("contractor");
while(!contractIterator.isLastEmployee()){
System.out.println("Contractor Employee : "+ contractIterator.nextEmployee().getEmpName());
}
Iterator permanentIterator = company.createIterator("permanent");
while(!permanentIterator.isLastEmployee()){
System.out.println("Permanent Employee : "+ permanentIterator.nextEmployee().getEmpName());
}
}
}
OutPut :
Contractor Employee : c1
Contractor Employee : c2
Permanent Employee : p1
Permanent Employee : p2
Permanent Employee : p3
Permanent Employee : p4
No comments:
Post a Comment