Design Pattern

Iterator Pattern Design Patterns in Java

<p>The <strong>iterator pattern<&sol;strong> comes under the <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;">behavioral patterns<&sol;a><&sol;strong>&period; The design uses iterator object&comma; which stores the track of the current object in the list and the next object which is supposed to be iterated next&period; Iterator pattern takes the responsibility for the accessing and passing of the objects through the list and storing them in an iterator object&period;<&sol;p>&NewLine;<h2>Iterator Pattern<&sol;h2>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>According to the Gang of Four&colon;<&sol;strong><&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation&period;<&sol;div>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<p>Iterator&comma; in the sense of computer programming&comma; is an object that allows the programmer to traverse a container&comma; specifically lists&period; The interface of the container is the source for different types of iterators&period; Iterators are often tightly linked to the container to allow the operational semantics of the iterator&period;<&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;observer-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;observer-pattern-design-patterns-java&sol;">Observer Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;chain-responsibility-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;chain-responsibility-pattern&sol;">Chain of responsibility Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;command-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;command-pattern-design-patterns-java&sol;">Command Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;strategy-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;strategy-pattern-design-patterns-java&sol;">Strategy Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;template-method-pattern-design-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;template-method-pattern-design-java&sol;">Template method Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interpreter-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interpreter-pattern-design-patterns-java&sol;">Interpreter Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mediator-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mediator-pattern-design-patterns-java&sol;">Mediator Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;memento-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;memento-pattern-design-patterns-java&sol;">Memento Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;&sol;">Creational GoF Design Patterns<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;">Structural GoF Design Patterns<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<p>To design&comma; versatile and reusable object-oriented software&comma; there are <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;design-patterns&lowbar;25&sol;">23 well-known design patterns<&sol;a>&comma; the iterator pattern is one of them&period; The idea of Iterator pattern is to give a way to access the elements of the comprehensive object in a sequential manner without having the need to know about its hidden representation&period; For example&comma; if a person writes a simple binary search algorithm in Java language&period; Due to the strong language used&comma; the function signature is most likely to couple the algorithm to the particular collection types&period; To decouple the collection types from the algorithm&comma; the iterator interface is required&period; This allows the algorithm to cover various types of data structures&period; This way&comma; the user does not need to write separate algorithms for each data type and can make the algorithm functions for more than one data type&comma; just by recycling it&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Patterns<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2>Benefits of using the Iterator pattern<&sol;h2>&NewLine;<p>There are following benefits of the Iterator pattern&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Easily access the items of the collection&period;<&sol;li>&NewLine;<li>You can use multiple to access the item from the collection&comma; because it support lot of variations in the traversal&period;<&sol;li>&NewLine;<li>It provides a uniform interface for traversing different structures in a collection&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>The iterator pattern is used in the object-oriented programming&period; It is the kind of design pattern where an iterator is used to traverse through a container and its elements&comma; this helps in decoupling the algorithm and the elements of the container&period; Though&comma; in some cases&comma; it is not possible to decouple the algorithms because they are specified by the container &lpar;depends directly on it&rpar;&period; The pattern prevents from the representation &lpar;data structures&rpar; of the container&comma; to be exposed&period; The procedure can easily be performed with the representation&comma; hidden&period; For each new data type&comma; there will be no need for new algorithms to be defined&period; Defining and writing down new algorithms for each different data type can consume time and energy both&period; The iterator pattern helps save time and energy&period;<&sol;p>&NewLine;<p>Characterizing entry and traversal operations in the comprehensive interface may be unbendable in order that it commits the collection to specific access and traversal operations&period; This makes it almost impossible to add new operations in future&comma; not unless the collection interface has been changed in respective order&period;<&sol;p>&NewLine;<p>The iterator pattern helps to define a separate object&comma; iterator&comma; that takes responsibility to access and traverse the collection object&period; This way&comma; other objects do not get jumbled up&period; The clients that use an iterator&comma; in order to access or traverse the container or elements of the container&comma; will not know about its underlying data structure or representation&period; Various types of iterators can be used to access and traverse in different ways&period; By declaring new iterators&comma; new traversal operations can be defined&period;<&sol;p>&NewLine;<h2>UML Class Structure<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following UML diagram is showing the all components of Iterator design pattern&colon;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-3298 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;iterator&period;gif" alt&equals;"Iterator Pattern UML diagram" width&equals;"381" height&equals;"283" &sol;><&sol;p>&NewLine;<p><strong>Iterator<&sol;strong><br &sol;>&NewLine;It is an interface or abstarct class for accessing and traversing items of the collections&period;<br &sol;>&NewLine;<strong>ConcreteIterator<&sol;strong><br &sol;>&NewLine;It is implementation of the Iterator interface&period;<br &sol;>&NewLine;<strong>Aggregate<&sol;strong><br &sol;>&NewLine;It is an interface to create an Iterator object<br &sol;>&NewLine;<strong>ConcreteAggregate<&sol;strong><br &sol;>&NewLine;It is implementation of the Aggregate interface&comma; it implements the Iterator creation interface to return an instance of the proper ConcreteIterator&period;<&sol;p>&NewLine;<h2>Example of Iterator Design Pattern<&sol;h2>&NewLine;<p>java&period;util&period;Iterator interface uses Iterator Design Pattern&period;<&sol;p>&NewLine;<p><strong>Step 1&colon; Create interfaces&period;<&sol;strong><&sol;p>&NewLine;<p><strong>Iterator&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">public interface Iterator &lbrace; &NewLine; public boolean hasNext&lpar;&rpar;&semi; &NewLine; public Object next&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><strong>Step 2&colon; Container&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">public interface Container &lbrace; &NewLine; public Iterator getIterator&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><strong>Step 3&colon; Create concrete class implementing the Container interface&period; <&sol;strong><&sol;p>&NewLine;<p>This class has an inner class <em><strong>NameCollectionIterator<&sol;strong><&sol;em> implementing the Iterator interface&period;<&sol;p>&NewLine;<p><strong>NameCollection&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">public class NameRepository implements Container &lbrace; &NewLine; public String names&lbrack;&rsqb; &equals; &lbrace;"Dinesh"&comma; "Anamika"&comma;"Arnav"&comma; "Adesh"&comma; "Vinesh"&rcub;&semi; &NewLine; &NewLine; &commat;Override &NewLine; public Iterator getIterator&lpar;&rpar; &lbrace; &NewLine; return new NameCollectionIterator&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; private class NameCollectionIterator implements Iterator &lbrace; &NewLine; &NewLine; int index&semi; &NewLine; &NewLine; &commat;Override &NewLine; public boolean hasNext&lpar;&rpar; &lbrace; &NewLine; &NewLine; if&lpar;index &lt&semi; names&period;length&rpar;&lbrace; &NewLine; return true&semi; &NewLine; &rcub; &NewLine; return false&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;Override &NewLine; public Object next&lpar;&rpar; &lbrace; &NewLine; &NewLine; if&lpar;this&period;hasNext&lpar;&rpar;&rpar;&lbrace; &NewLine; return names&lbrack;index&plus;&plus;&rsqb;&semi; &NewLine; &rcub; &NewLine; return null&semi; &NewLine; &rcub;&Tab;&Tab; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><strong>Step 4&colon; Create a IteratorPatternDemo class&period;<&sol;strong><&sol;p>&NewLine;<p><strong>IteratorPatternDemo&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">public class IteratorPatternDemo &lbrace; &NewLine;&Tab; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; NameRepository namesRepository &equals; new NameRepository&lpar;&rpar;&semi; &NewLine; &NewLine; for&lpar;Iterator iter &equals; namesRepository&period;getIterator&lpar;&rpar;&semi; iter&period;hasNext&lpar;&rpar;&semi;&rpar;&lbrace; &NewLine; String name &equals; &lpar;String&rpar;iter&period;next&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Name &colon; " &plus; name&rpar;&semi; &NewLine; &rcub; &Tab; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><strong>Step 4&colon; Let&&num;8217&semi;s run this demo class and verify the output&period;<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">Name &colon; Dinesh &NewLine;Name &colon; Anamika &NewLine;Name &colon; Arnav &NewLine;Name &colon; Adesh &NewLine;Name &colon; Vinesh &NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;design-patterns&lowbar;25&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mediator-pattern-design-patterns-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '3297'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago