Start course
4h 56m

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.



Hello dear friends. In this video, we will examine Iterator Design Pattern. Let's begin. 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. As the name implies, the iterator helps in traversing the collection of objects in a defined manner, which is useful for the client applications. During iteration, client programs can perform various other operations 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. Iterative 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 elements collection, provide methods that concrete iterators must implement. Concrete iterator implements the iterator interface method. 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 it's specific method returns an instance of concrete iterator. Iterator design pattern is a relatively simple design pattern which we use in almost 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 or collection, that makes our collection object easier to implement, change, test, and reuse. Java has the iterator interface. 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 showing any list to users. So, I will show names with the help of this pattern.

First, we need an interface, we'll call it iterator. Now in 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's Zweig, and of course, Dostoyeviski, if you want, you can add more names to your list. Let's override 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 loop, now we can call next method, assign names in a string variable. Now we can show. Done. Let's look at the result. Look, we can see the author's names. I hope you now have a better understanding of the iterator pattern. In the following video, we will look at the visitor pattern. See you in the following video.


About the Author
Learning Paths

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.

Covered Topics