CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Template Method

Iterator Design Pattern

Iterator design pattern is used to access and traverse an aggregate object without exposing its internal structure.

  • Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
The intent is to provide the access of aggregate object without exposing its internal structure.

  • Need to access the elements of an aggregate object without exposing the internal details of object.
Accessing elements of object is required with traversal, and at the same time it should not expose the internal structure of aggregate object. Also this may be applicable to different data structures, which provide different algorithms for accessing and traversing.

  • Provide a way to access and traverse an aggregate object by giving responsibility of access and traversal to another object.
So there can be common interface, which can be applicable for different data structures to provide interface for accessing and traversing. There will be separate objects having this interface and will have responsibility to access the elements and traversal of aggregate objects.

Where it is applicable?
  • Access and traversal of aggregate object is required without exposing its internal structure.
  • Common interface is required to access and traverse different data structures.

Iterator Design Pattern

Participant classes
  • Iterator class provides the interface for accessing the elements and traversal of the aggregate object.
  • ConcreteIterator class implements the interface of Iterator class and keeps track of current item of aggregate object.
  • Aggregate class provides the interface for creating Iterator object.
  • ConcreteAggregate class implements the method CreateIterator().
How they work together?
  • The client creates the ConcreteAggregate object. It uses the CreateIterator interface to create the ConcreteIterator object. This will have the current item information to track the aggregate object. Now it uses the Iterator interface to access the elements and traversal of aggregate object.
  • So we can have different data structures, and their concrete iterators can implement interface according to their traversal algorithm. This makes the access and traversal simple for different data structures.

Implementation Code

C++   Iterator Pattern in C++   Example in C++
C#   Iterator Pattern in C#   Example in C#
Java   Iterator Pattern in Java   Example in Java
Python   Iterator Pattern in Python   Example in Python
JavaScript   Iterator Pattern in JavaScript   Example in JavaScript
PHP   Iterator Pattern in PHP   Example in PHP
Ruby   Iterator Pattern in Ruby   Example in Ruby
Swift   Iterator Pattern in Swift   Example in Swift
Objective-C   Iterator Pattern in Objective-C   Example in Objective-C

Suresh Kumar Srivastava is founder of online learning site and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his courses on design patterns.

Yearly Subscription

Design Patterns in Ruby

Programming in C#




Knowledge Hub

Design Patterns

Tech Articles

About Us


Contact Us


CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy