The Iterator Design Pattern is one of the behavioral
design patterns. It provides a standard way to traverse or iterate through
a collection of objects (like lists, trees, or other data structures) without
exposing the underlying representation of the collection.
Intent
- To
access elements of a collection sequentially without exposing its
underlying representation.
Key Components
- Iterator
(Interface)
- Defines
the interface for accessing and traversing elements.
- Example
methods: hasNext(), next()
- Concrete
Iterator
- Implements
the Iterator interface.
- Keeps
track of the current position in the traversal.
- Aggregate
/ Collection (Interface)
- Defines
the interface for creating an iterator object.
- Concrete
Aggregate
- Implements
the Aggregate interface.
- Returns
an instance of the concrete iterator.
📘 Advantages
- Hides
the internal structure of collections.
- Provides
a uniform interface for traversing different collection types.
- Supports
multiple simultaneous iterations on the same collection.
⚠️ Disadvantages
- Adds
complexity in custom collections.
- Might
lead to overhead if used in simple structures.
✅ Java Built-in Support
Java provides built-in support for this pattern through the
Iterator interface found in the java.util package.
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element
= iterator.next();
System.out.println(element);
}
Example :
Step 1: Create the Iterator interface
package iteratordesignpattern;
public interface Iterator {
boolean
hasNext();
Object next();
}
Step 2: Create the Container interface
package iteratordesignpattern;
public interface Container {
Iterator
getItertaor();
}
Step 3: Create NameRepository class
package iteratordesignpattern;
public class NameRepository implements
Container {
public
String[] names = { "Govind", "Ballabh", "Khan", "Java",
"Technology" };
@Override
public
Iterator getItertaor() {
//
TODO Auto-generated method stub
return new NameIterator();
}
private
class NameIterator implements Iterator {
int
index;
@Override
public
boolean hasNext() {
//
TODO Auto-generated method stub
return
index < names.length;
}
@Override
public
Object next() {
if
(this.hasNext()) {
return
names[index++];
}
return
null;
}
}
}
Step 4: Test the Iterator
package iteratordesignpattern;
public class IteratorPatternDemo {
public
static void main(String[] args) {
NameRepository
nameRepository = new NameRepository();
for
(Iterator iterator = nameRepository.getItertaor(); iterator.hasNext();) {
String name
= (String)iterator.next();
System.out.println(name);
}
}
}
o/p:
Govind
Ballabh
Khan
Java
Technology
Example :2
package iteratordesignpattern;
public class Book {
private
String title;
private
String author;
public
Book(String title, String author) {
super();
this.title
= title;
this.author
= author;
}
public
String getTitle() {
return
title;
}
public void
setTitle(String title) {
this.title
= title;
}
public
String getAuthor() {
return
author;
}
public void
setAuthor(String author) {
this.author
= author;
}
}
package iteratordesignpattern;
public interface Iterator {
boolean
hasNext();
Object next();
}
package iteratordesignpattern;
public interface Container {
Iterator
getItertaor();
}
package iteratordesignpattern;
public class BookRepository implements
Container {
private
int count = 0;
private
Book[] books;
public
BookRepository(int size) {
books
= new Book[size];
}
public
void addBook(Book book) {
if
(count < books.length) {
books[count]
= book;
count++;
}
}
@Override
public
Iterator getItertaor() {
return
new BookIterator();
}
public
class BookIterator implements Iterator {
private
int index = 0;
@Override
public
boolean hasNext() {
//
TODO Auto-generated method stub
return
index < count && books[index] != null;
}
@Override
public
Object next() {
if
(this.hasNext()) {
return
books[index++];
}
return
null;
}
}
}
package iteratordesignpattern;
public class BookIteratorDemo {
public
static void main(String[] args) {
BookRepository
bookRepository = new BookRepository(5);
bookRepository.addBook(new
Book("OS", "Galvin"));
bookRepository.addBook(new
Book("Java", "Durga Sir"));
bookRepository.addBook(new
Book("C++", "Yashwantkanetkar"));
Iterator
iterator = bookRepository.getItertaor();
while
(iterator.hasNext()) {
Book
book = (Book) iterator.next();
System.out.println("Book:
" + book.getTitle() + ", Author: " + book.getAuthor());
}
}
}
o/p:
Book: OS, Author: Galvin
Book: Java, Author: Durga Sir
Book: C++, Author: Yashwantkanetkar
No comments:
Post a Comment