top of page
  • Writer's pictureSunil Kumar Yadav

Iterator: Must Know Design Pattern (5/7)




The Iterator Pattern is a behavioral design pattern that provides a way to access the elements of an aggregate object (a collection) sequentially without exposing its underlying representation. It allows you to traverse the elements of a collection, such as an array, list, or tree, in a consistent and easy-to-use manner.



A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collection have start and end which can be iterated over. Array, List are common example of collection.

What is Iterator Pattern?


The iterator pattern provides an easy way to access the elements of an aggregate object sequentially without exposing its underlying representation. Let's try to understand how the Iterator pattern can be used with the help of a simple Linked List example.


class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self, head):
        self.head = head
        self.curNode = None

    # Define Iterator. It's a special dunder method
    def __iter__(self):
        self.curNode = self.head
        return self

    # Iterate using special dunder method
    def __next__(self):
        if self.curNode:
            val = self.curNode.val
            self.curNode = self.curNode.next
            return val
        else:
            raise StopIteration

Now we can initialize the LinkedList by adding a few elements and then using for loop we can iterate through each element, like we do for an inbuilt list.

# Initialize LinkedList by adding few elements
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)

myList = LinkedList(head) 
 
# Iterate through linked list
for n in myList:
    print(n)

The Iterator Pattern offers several benefits:

  1. Separation of Concerns: It separates the iteration logic from the underlying data structure, promoting a clean and organized code structure.

  2. Simplified Client Code: Clients that need to iterate through a collection can do so without needing to know the details of the collection's internal representation. This simplifies client code and reduces dependencies.

  3. Support for Multiple Iterators: Collections can offer multiple iterators with different traversal behaviors, allowing clients to choose the most appropriate iterator for their specific needs.

  4. Consistent Interface: Iterators provide a uniform interface for traversing various types of collections, making it easier to work with different data structures.


Where Iterator pattern is used?


The Iterator Pattern is commonly used in various real-world scenarios where you need to iterate over a collection of objects without exposing the underlying structure of that collection. Here are some real-world examples:

  1. Programming Language Libraries: Most programming languages provide built-in support for the Iterator Pattern. For instance, in Python, you can use iterators to traverse lists, dictionaries, and other built-in data structures without knowing the details of their implementations.

  2. Database Query Results: Database libraries and ORMs (Object-Relational Mapping) often use the Iterator Pattern to retrieve query results. It allows you to iterate over query results without fetching the entire result set into memory at once.

  3. File System Navigation: When working with file systems, you can use an iterator to navigate through directories and files. It's useful for tasks like searching for specific files or generating file listings.

  4. Streaming Services: Streaming services like Netflix, Spotify, or YouTube use iterators to manage playlists and video queues. You can move from one item to the next in the list without knowing the full list's structure.

  5. GUI Components: Graphical user interface frameworks often use iterators for managing lists of elements in forms, dropdown menus, and tree views. Users can navigate through these lists without knowledge of the underlying data structures.

  6. Social Media Feeds: Social media platforms like Facebook and Twitter use iterators to let users scroll through their news feeds. Each feed item is an element in the collection, and users can navigate through them.

  7. Text Editors: Text editors often use iterators to move through text documents. You can move through lines, paragraphs, or characters in a text document without needing to parse the entire document at once.

  8. Machine Learning Datasets: When working with large datasets in machine learning and data science, iterators can be used to process data in chunks or sequences, which is memory-efficient for handling massive datasets.

In each of these instances, the Iterator Pattern serves to uphold a distinct division between the group of objects and the logic responsible for iterating through them. This distinction simplifies the process of working with collections of different sizes and structures, all the while preventing the client code from becoming intricately entwined with the inner workings of the collection's implementation.

15 views0 comments

Recent Posts

See All
bottom of page