Saturday 26, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 29 July 2017

Design Patterns - Iterator Pattern

Iterator Pattern

Definition

The Iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Where to use

Use to access the elements of an aggregate object sequentially. Java's collections like ArrayList and HashMap have implemented the iterator pattern.

Benefits

•The same iterator can be used for different aggregates.
•Allows you to traverse the aggregate in different ways depending on your needs.
•It encapsulates the internal structure of how the iteration occurs.
•Don't need to bloat the your class with operations for different traversals.

Drawbacks/consequences

Not thread safe unless its a robust iterator that allows insertions and deletions. This can be be solved by letting the Iterator use a Memento to capture the state of an iteration.

Iterator Pattern Class Diagram


Iterator Pattern Example


This example shows how you can write your own iterator.

import java.util.*;
public class BitSetIterator implements Iterator {
private final BitSet bitset;
private int index;
public BitSetIterator(BitSet bitset) {
this.bitset = bitset;
}
public boolean hasNext() {
return index < bitset.length();
}
public Boolean next() {
if (index >= bitset.length()) {
throw new NoSuchElementException();
}
boolean b = bitset.get(index++);
return new Boolean(b);
}
public void remove() {
throw new UnsupportedOperationException();
}
}

public class TestClientBitSet {
public static void main(String[] args) {
// create BitSet and set some bits
BitSet bitset = new BitSet();
bitset.set(1);
bitset.set(19);
bitset.set(20);
bitset.set(47);
BitSetIterator iter = new BitSetIterator(bitset);
while (iter.hasNext()) {
Boolean b = iter.next();
String tf = (b.booleanValue() ? "T" : "F");
System.out.print(tf);
}
System.out.println();
}
}
When TestClientBitSet is executed the result is:
c:>FTFFFFFFFFFFFFFFFFFTTFFFFFFFFFFFFFFFFFFFFFFFFFFT

Usage example

This example shows how the Iterator of the ArrayList is used.

import java.util.*;
public class TestClientIterator {
public static void main(String[] args) {
ArrayList<Object> al = new ArrayList<Object>();
al.add(new Integer(42));
al.add(new String("test"));
al.add(new Double("-12.34"));
for(Iterator<Object> iter=al.iterator();
iter.hasNext();)
System.out.println( iter.next() );
// JEE5 syntax
for(Object o:al)
System.out.println( o );
}
}
When run the result is:
c:>42
c:>test
c:>-12.34
c:>42
c:>test
c:>-12.34

In the following class-diagram we illustrate the structure from the usage example of Iterator Pattern.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Design Patterns - Composite Pattern Composite Pattern Definition The Composite pattern helps you to create tree structures of objects without the need to force clients to differentiate between branches and leaves regarding usage. The Composite pattern lets c… Read More
  • Design Patterns - Decorator Pattern Decorator Pattern Definition The Decorator pattern lets you attach additional responsibilities and modify an instance functionality dynamically. Decorators provide a flexible alternative to subclassing for extending functi… Read More
  • Design Patterns - Singleton Pattern Singleton Pattern Definition The Singleton pattern provides the possibility to control the number of instances (mostly one) that are allowed to be made. We also receive a global point of access to it (them). Where to use … Read More
  • Design Patterns - Command Pattern Command Pattern Definition The Command pattern is used to create objects that represents actions and events in an application. A command object encapsulates an action or event and contains all information required to under… Read More
  • Design Patterns - Chain-of-responsibility Pattern Chain-of-responsibility Pattern The Chain-of-responsibility pattern lets more than one object handle a request without mutual knowledge. We avoid coupling between the sender of a request and the possible receivers. We place… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Iterator Pattern Rating: 5 Reviewed By: eHowToNow