🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Definition
The Iterator Design Pattern provides a way to access the elements of an aggregate object (like collections) sequentially without exposing its underlying representation. It allows for the creation of custom iteration logic tailored to specific collections.
2. Problem Statement
When you need to provide multiple ways to iterate over a collection or when the collection's underlying structure is complex, directly accessing elements becomes tricky. Exposing the inner workings of the collection might also risk data integrity and tight coupling.
3. Solution
Introduce an iterator object that encapsulates the logic required for iteration. This iterator knows how to access elements in the collection, one at a time, without the client needing to know the collection's internal structure.
4. Real-World Use Cases
1. Navigating through a playlist in a media player.
2. Browsing through pages in a book or a digital reader.
3. Iterating over different tree or graph structures.
5. Implementation Steps
1. Create an Iterator interface that defines methods like hasNext() and next().
2. Create a concrete iterator for each collection class.
3. The collection class should have a method to return its iterator.
6. Implementation in Kotlin Programming
// Step 1: Define the Iterator interface
interface Iterator<T> {
fun hasNext(): Boolean
fun next(): T?
}
// Step 2: Concrete Iterator
class BookShelfIterator(private val bookShelf: BookShelf) : Iterator<Book> {
private var index = 0
override fun hasNext(): Boolean {
return index < bookShelf.getLength()
}
override fun next(): Book? {
return bookShelf.getBookAt(index++)
}
}
// Aggregate class
class BookShelf : Iterable<Book> {
private val books = mutableListOf<Book>()
fun addBook(book: Book) {
books.add(book)
}
fun getLength() = books.size
fun getBookAt(index: Int) = books[index]
override fun iterator(): Iterator<Book> = BookShelfIterator(this)
}
data class Book(val name: String)
// Client Code
fun main() {
val bookShelf = BookShelf()
bookShelf.addBook(Book("Design Patterns"))
bookShelf.addBook(Book("Effective Java"))
bookShelf.addBook(Book("Clean Code"))
val iterator = bookShelf.iterator()
while (iterator.hasNext()) {
val book = iterator.next()
println(book?.name)
}
}
Output:
Design Patterns Effective Java Clean Code
Explanation:
1. We defined an Iterator interface with essential methods: hasNext() and next().
2. BookShelfIterator is a concrete implementation of this interface, tailored to work with the BookShelf class.
3. The BookShelf class, representing our collection, implements the Iterable interface and returns its custom iterator.
4. The client code demonstrates how to iterate over the BookShelf collection using the BookShelfIterator.
7. When to use?
Use the Iterator pattern when:
1. You want to provide a standardized way to traverse different collections.
2. The internal structure of your collection is complex, and you want to hide the complexity from clients.
3. You need to provide multiple styles of iteration over the same collection.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment