Iterator Design Pattern
Start course
Difficulty
Beginner
Duration
3h 59m
Students
131
Ratings
5/5
starstarstarstarstar
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello, my dear friends. In this video, we will examine Iterator Design Pattern. So, let's begin. In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse the 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. As the name implies, the iterator helps in traversing the collection of objects in a defined manner which is useful for client applications. During iteration, client programs can perform various other options on the elements as per requirements. This pattern is used to get away to access the elements of a collection object in a sequential manner without any need to know its underlying representation. Iterator pattern falls under the behavioral pattern category. The iterator pattern is a relatively simple and frequently used design pattern. There are a lot of data structures/collections available in every language.

Each collection must provide an iterator that lets it iterate through its objects. However, while doing so, it should make sure that it does not expose its implementation. Iterator allows sequential traversal through a complex data structure without exposing its internal details. For that reason, the iterator pattern is also known as cursor. According to the GoF definition, an iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. The iterator design pattern is one of 23 well known GoF design patterns. As you see in the UML of iterator, there are four participants. Iterator is an interface to access or traverse the element's collection. It provides methods that concrete iterators must implement.

Concrete iterator implements the iterator interface methods. It can also keep track of the current position in the traversal of the aggregate collection. Aggregate is typically a collection interface which defines a method that can create an iterator object. Concrete aggregate implements the aggregate interface and its specific method returns an instance of ConcreteIterator. Iterator design pattern is a relatively simple design pattern, which we use almost in every project. Few containers require an algorithm to access the member elements. Iterator design pattern decouples any such algorithm from the container. The algorithm can be written separately to use by the iterator and hence can be used by any container which supports that kind of iterator object. The iterator design pattern provides a flexible and reusable solution of traversing member objects of a container/collection. That makes our collection object easier to implement, change, test, and reuse. Java has the iterator interface.

So now, let's apply the iterator pattern to a little project. Create a new project and name it Iterator. Now, the most used example of an iterator pattern is to show a list to the users. So, I will show names with the help of this pattern. First, we need an interface, we'll call it Iterator. Now, in the interface we need two methods, hasNext() and next(). Let's add container interface. Define getIterator. Now, we need a name list and for this, I will add a collection. NameCollection implements Container. Let's define our list. I want to use well known author names. Let's add authors like Charles Dickens or John Verdon, Stefan Zweig, and of course, Dostoyevsky. If you want, you can add more names to your list. Let's override the getIterator method. Also, we have to override the other methods, hasNext(), and next().

Now our pattern is ready. Let's get values from the list. First, define an instance from collection names. Now we need a loop, you can use any loop. I like to use while but I will use while in another project. So, now let's use for loop. In the loop now, we can call the next method. Assign names in a string variable. Now we can show. Done. Okay, let's look at the results. Look, we can see the authors names. So, I hope you have a better understanding of the iterator pattern. In the following video, we will look at the visitor pattern. So, I'll see you in the next video.

 

About the Author
Students
1996
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.