Iterator Design Pattern

What is Iterator Design Pattern?

  • It falls under behavioral design pattern.
  • It accesses the elements of a collection object sequentially without any need to know its underlying representation.
  • The collection can be a List, Set, Array and it can be anything.
  • In OOPs,  Iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements.
  • Abstract the details of traversing the collections. Example: Different types of collections in an application such as an array, linked list, etc. We can traverse and iterate the elements without knowing the actual implementation.

Real Life Examples of Iterator Design Pattern

Example 1:

UML Class Diagram of Iterator Design Pattern

Iterator Design Pattern Java Implementation

Step 1:

public class Voltage
{

    private int voltage;

    public Voltage(int v)
    {
        this.voltage = v;
    }

    public int getVolts()
    {
        return voltage;
    }

    public void setVolts(int voltage)
    {
        this.voltage = voltage;
    }

}

Tutorial Videos of Iterator Design Pattern

For tutorial videos, please check out the premium Software Design Pattern course on Udemy.  

Share