Design pattern "Iterator"
Real world example
In a music player, you often have a playlist which is essentially a collection of songs. Now, one of the most common operations on this playlist is to go through the songs one by one in a certain order (maybe sequentially or in shuffle mode). This is where an iterator becomes very useful.
Hereβs a simplified example:
The iterator pattern here allows the MusicPlayer
to not worry about how the Playlist is implemented. The playlist could be a simple list, a queue, a stack, or any other data structure that fits the requirements. The MusicPlayer
simply asks for the next song from the iterator. This makes the MusicPlayer
code simpler, more readable, and easier to maintain. It abstracts away the details of accessing elements from a collection.
Note: We can use a while
loop too to iterate !
In plain words
It presents a way to access the elements of an object without exposing the underlying presentation.
Wikipedia definition
In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the containerβs elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
Programmatic example
In this example, MyCollection
is a collection that implements the Iterable
interface. MyIterator
is an inner class of MyCollection
that implements the Iterator
interface. The main
method creates a MyCollection
object and uses a for-each loop to iterate over its elements. The hasNext
method checks if there are more elements to iterate over, and the next
method returns the next element. This is a basic example of the Iterator design pattern in Java. In a real-world application, the collection could be any type of data structure, such as a list, set, or map, and the iterator could be used to implement complex traversal algorithms.
Diagram
Loading graph...
Some other examples
- File System Traversal: When you need to traverse through a file system, which can be seen as a tree-like structure (directories contain other directories and files), an iterator can be used to traverse through all the files.
- Social Networking Platforms: In a social network, iterators can be used to traverse through the list of friends, followers, posts, comments, etc.
- Music Player: In a playlist of a music player, an iterator can be used to go through each song one by one.
- Database Query Result: When a database query returns a set of records, an iterator can be used to go through each record.
- Image Pixel Processing: In image processing, an iterator can be used to traverse through each pixel of an image for processing.
- Text Processing: In a text document, an iterator can be used to traverse through each character or word in the document.
- Graph Traversal: In graph data structures (used in maps, social networks etc.), iterators can be used to traverse nodes using algorithms like Depth-First Search (DFS) or Breadth-First Search (BFS).
- Data Structures: In data structures like arrays, linked lists, trees, hash maps, etc., iterators are used to traverse through the elements.
- Web Scraping: When scraping data from a website, an iterator can be used to go through each HTML element.
- Game Development: In a game, an iterator can be used to cycle through game objects or game states.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ππππ
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "State"
Let's learn what is the "State" design pattern ππ»ββοΈπΆπ»ππ»
Design pattern "Adapter"
Let's learn what is the "Adapter" design pattern π