Showing posts with label Iterator Design Pattern. Show all posts
Showing posts with label Iterator Design Pattern. Show all posts

Iterator Design Pattern in Java

Iterator Design Pattern in Java

Iterator pattern is a behavioral object design pattern. Iterator pattern allows for the traversal through the elements in a grouping of objects via a standardized interface defines the actions or methods that can be performed. These actions or methods able to traverse the objects, remove or add Object according to project needs. So keeping in mind about the pattern just we need to apply on top of our business logic's. 

If we look at Java widely used Iterator interface which used to iterate through elements in various Java collections. We can write our own iterator by implementing java.util.Iterator. This interface features the hasNext(), next(), and remove() methods. Lets take simple example of book stall which contains list of books with these few following information's like,

Book Name
Author
Price

Based on above example lets create Book class which holds the book details


public class Book {

 private String name;
 private String author;
 private int price;
 
 public Book(String name, String author, int price) {
  this.name = name;
  this.author = author;
  this.price = price;
 }
 public String getName() {
  return name;
 }
 public String getAuthor() {
  return author;
 }
 public int getPrice() {
  return price;
 }

 @Override
 public String toString() {
  return "----------\n\nBook Name : "+name+" \nAuthor    : "+author+"\nPrice     : Rs."+price; 
 } 
}




Next lets create Store class which contains list of various books and list actions like add books, removing, listing etc., can be performed. 


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Store {
 
 List<Book> bookList = new ArrayList<Book>();
 
 public void addBook(Book item) {
  bookList.add(item);
 }

 public Iterator<Book> iterator() {
  return new BooksIterator();
 }

 class BooksIterator implements Iterator<Book> {
  int bookId = 0;

  @Override
  public boolean hasNext() {
   if (bookId >= bookList.size()) {
    return false;
   } else {
    return true;
   }
  }

  @Override
  public Book next() {
   return bookList.get(bookId++);
  }

  @Override
  public void remove() {
   bookList.remove(--bookId);
  }
 } 
}


Next lets implement the main class which feeds list of books into Store and iterates. For sample lets add 4 books and will test.


import java.util.Iterator;

public class BookStore {
 
 public static void main(String[] args) {
  Book b1 = new Book("Lightning", "Dean Koontz", 500);
  Book b2 = new Book("Roma: The Novel of Ancient Rome", "Steven Saylor", 452);
  Book b3 = new Book("Humility", "Andrew Murray", 334);
  Book b4 = new Book("Meditations", "Marcus Aurelius, Martin Hammond", 635);
  
  Store store = new Store();
  store.addBook(b1);
  store.addBook(b2);
  store.addBook(b3);
  store.addBook(b4);
  
  System.out.println("Display list of books in store");
  Iterator<Book> iterator = store.iterator();
  while (iterator.hasNext()) {
   Book item = iterator.next();
   System.out.println(item.toString());
   
   //Removing "Humility" from the book store list
   if(item.getName().equals("Humility")){
    iterator.remove();
   }
  }
  
  System.out.println("\n\n\nDisplay list of books in store after removing 'Humility' book");
  iterator = store.iterator();
  while (iterator.hasNext()) {
   Book item = iterator.next();
   System.out.println(item.toString());
  }
 }
}


OUTPUT:


Display list of books in store
----------

Book Name : Lightning 
Author    : Dean Koontz
Price     : Rs.500
----------

Book Name : Roma: The Novel of Ancient Rome 
Author    : Steven Saylor
Price     : Rs.452
----------

Book Name : Humility 
Author    : Andrew Murray
Price     : Rs.334
----------

Book Name : Meditations 
Author    : Marcus Aurelius, Martin Hammond
Price     : Rs.635



Display list of books in store after removing 'Humility' book
----------

Book Name : Lightning 
Author    : Dean Koontz
Price     : Rs.500
----------

Book Name : Roma: The Novel of Ancient Rome 
Author    : Steven Saylor
Price     : Rs.452
----------

Book Name : Meditations 
Author    : Marcus Aurelius, Martin Hammond
Price     : Rs.635