Iterator method is a Behavioral Design Pattern which allows us to traverse the elements of the collections without taking the exposure of in-depth details of the elements. It provides a way to access the elements of complex data-structure sequentially without repeating them.
According to GangOfFour, Iterator Pattern is used ” to access the elements of an aggregate object sequentially without exposing its underlying implementation”.
Following diagram depicts Linked List data structure.
Problem Without using Iterator Method
Imagine you are creating an application for small kids which takes any valid alphabet as input and return all the alphabets up to that. When this application will be used only few times, it is okay to run For loop and While loop again and again but when the frequency of running increases this process becomes quite inefficient. So, we have to find a way in order to avoid these loops. This problem may become bigger when we will work on complex non-linear data structures like Trees, Graphs where traversing is not that simple as in an array.
The following diagram depicts the image of the Tree data structure.
Solution Using Iterator Method
Here we will discuss the solution for the above-described problem. It’s always handy for Python users to use Iterators for traversing any kind of data structure doesn’t matter they are linear or no-linear data structures. We have two options to implement Iterators in Python either we can use the in-built iterators to produce the fruitful output or explicitly we can create iterators with the help of Generators. In the following code, we have explicitly created the Iterators with the help of generators.
Note: Following code is the example of explicitly created Iterator method
Note: Following code is the example of using in-built iterator method
Following is the class diagram for the Iterator Method
- Single Responsibility Principle: It’s really easy to extract the huge algorithms into the separate classes in Iterator method.
- Open/Closed Principle: Passing the new iterators and collections into the client code does not break the code can easily be installed into it.
- Easy to use Interface: It makes the interface really simple to use and also supports the variations in the traversal of the collections.
- Unnecessary Wasting resources: Its not always a good habit to use the Iterator Method because sometimes it may proves as a overkill of resources in a simple application where complex things are not required.
- Increases Complexity: As we said earlier, using Iterator method makes simple applications complex.
- Decreases Efficiency: Accessing elements directly is a much better option as compared to accessing elements using the iterator in terms of efficiency .
- Limited Exposure: When you want to access the elements at the lower level i.e., you are not interested in the internal implementation of the elements then it’s always preferred to use Iterator Method.
- Traversing Unknown Data Structures: Iterator method can be easily used to traverse various types of data structures such as Trees, Graphs, stack, Queue etc. as the code provides the couple of generic interfaces for both collections and iterators.
Further Read – Iterator Method in Java
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Factory Method - Python Design Patterns
- Observer method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Abstract Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Singleton Method - Python Design Patterns
- Adapter Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- Memento Method - Python Design Patterns
- Visitor Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Template Method - Python Design Patterns
- State Method - Python Design Patterns
- Command Method - Python Design Patterns
- Chain of Responsibility - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.